Does anyone know off hand how to prevent url.forma...
# suitescript
e
Does anyone know off hand how to prevent url.format from encoding the values in the options.params? In other words I am trying to build a url like this:
<https://fruitland.com?fruit=grape&seedless=true&variety=Concord:Giant>
However when I use url format like this
Copy code
apiUrl = url.format({
   domain: '<https://fruitland.com>',
   params: {
      fruit: grape
      seedless: true,
      variety: 'Concord:Giant'
   }
});
this is the result:
<https://fruitland.com?fruit=grape&seedless=true&variety=Concord>*%3A*Giant
k
%3A URL encoded is your colon. The string you typed above isn't a valid URL encoded string. The one coming out of url.format is correct.
e
Thanks Kurt, I am aware the %3A is the url encoded version of a ":" and agree that this is the proper format, however the third party api requires the ":" in the query parameter
k
You can't can't have two :'s in a URL - so your third party is out of compliance. You won't be able to use url.format - because it isn't going to let you create an invalid URL string.
You only get one colon per URL - and it has to come after http or https or ftp or the protocol. So, you'll have to manually build it with javascript - NOT using url.format.
a
Copy code
var funkyURLBuilder = (domain, params) => {
    var output = `${domain}?`; 
    for (param in params) {
        output += param;
        output += `=${params[param]}&`;
        
    }
    output = output.slice(0, -1); 
    return output;
}
e
thanks @Anthony OConnor this snippet I wrote also works
Copy code
const urlParams = {"name":"John", "age":30, "car":null};
var a = [];
for (const x in urlParams) {
 a.push(x+"="+urlParams[x]);
}
queryParams = a.join("&");
and is also a bit more "elegant" than
Copy code
apiUrl = url.format({
  domain: BASE_URL+endpoint,
  params: urlParams
}).replace("%3A",':');
✅ 1
ðŸĪŠ 2
a
lol well wasn't sure how generic it needed to be 😄
its weird that the endpoint doesn't decode urls tho
and actually wants invalid urls instead
e
ðŸĪ·â€â™‚ïļ I just work here
😂 1