function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hunter AllspaughHunter Allspaugh 

Is there an efficient way to convert a Map<String, String> to a url query?

I'm building a new class to act as an external connector to a project management service, AceProject.  The API is purely URL based, so I want to be able to make my http requests dynamically.  Trying to build a method to take the Map parameter and return it after it is converted into a string.

Currently, my solution is:
private static String buildHttpUrl(Map<String, String> params){
    String url = '';
			
    for (String key : params.keySet()){
        if (url.length() != 0){
            url += '&';
        }                
        url += key + '=' + params.get(key);
    } 
    return PRIVATE_STR_API + EncodingUtil.urlEncode(url, 'UTF-8');
}

Is there a way to do this without writing the math out?  I looked into String.join() but I don't know if I can make that work, since the format needs to be `&key=value`.
Raj VakatiRaj Vakati
I think what you are doing is correct and good .. even  String.join()  will be more complelicated to do how you want ..


I dnt see any issue in this code 
Hunter AllspaughHunter Allspaugh
Fair enough, thanks Raj.  I'll keep working with this.