A quick article on converting a Map (associative array with keys and values) into a URL string to pass as URL parameters.
How?
Let's first define a map:
m_Payload = Map(); m_Payload.put("client_id","my-unique-client-id"); m_Payload.put("redirect_uri","https://www.joellipman.com"); m_Payload.put("response_type","code"); m_Payload.put("scope","my_api_scopes"); m_Payload.put("prompt","login");
- m_Payload = Map();
- m_Payload.put("client_id","my-unique-client-id");
- m_Payload.put("redirect_uri","https://www.joellipman.com");
- m_Payload.put("response_type","code");
- m_Payload.put("scope","my_api_scopes");
- m_Payload.put("prompt","login");
v_Endpoint = "https://api.joellipman.com/myapi"; l_UrlParams = List(); for each v_Key in m_Payload.keys() { l_UrlParams.add(v_Key.toString() + "=" + m_Payload.get(v_Key)); } info v_Endpoint + "?" + l_UrlParams.toString("&"); // yields https://api.joellipman.com/myapi?client_id=my-unique-client-id&redirect_uri=https://www.joellipman.com&response_type=code&scope=my_api_scopes&prompt=login
- v_Endpoint = "https://api.joellipman.com/myapi";
- l_UrlParams = List();
- for each v_Key in m_Payload.keys()
- {
- l_UrlParams.add(v_Key.toString() + "=" + m_Payload.get(v_Key));
- }
- info v_Endpoint + "?" + l_UrlParams.toString("&");
- // yields https://api.joellipman.com/myapi?client_id=my-unique-client-id&redirect_uri=https://www.joellipman.com&response_type=code&scope=my_api_scopes&prompt=login
In Reverse
To have the reverse effect of converting a URL into a map, I'm using the following:
v_ReturnedUrl = "https://api.joellipman.com/myapi?isAuthSuccessful=true&code=my_own_code&expires_in=299"; l_ReturnedUrlParts = v_ReturnedUrl.subString(v_ReturnedUrl.indexOf("?")+1).toList("&"); m_UrlParams = Map(); for each r_Part in l_ReturnedUrlParts { if(r_Part.contains("=")) { l_ParamParts = r_Part.toList("="); m_UrlParams.put(l_ParamParts.get(0), l_ParamParts.get(1)); } }
- v_ReturnedUrl = "https://api.joellipman.com/myapi?isAuthSuccessful=true&code=my_own_code&expires_in=299";
- l_ReturnedUrlParts = v_ReturnedUrl.subString(v_ReturnedUrl.indexOf("?")+1).toList("&");
- m_UrlParams = Map();
- for each r_Part in l_ReturnedUrlParts
- {
- if(r_Part.contains("="))
- {
- l_ParamParts = r_Part.toList("=");
- m_UrlParams.put(l_ParamParts.get(0), l_ParamParts.get(1));
- }
- }