The CreatorCon Call for Content is officially open! Get started here.

How to add Query Parameters with space in SerivceNow Rest APi?

User477307
Tera Contributor

Hi there,

 

I'm trying to connect to a third party URL
where the query parameters are as follows:
@Param1 = All systems;
@param2 = action 3;

Ideally the url in POSTMAN is formed as
https://www.demoendpoint.com/welcome?@param1=All%20systems&@param2=action%203

But ServiceNow Script which I'm using below:
var demotest= new sn_ws.RESTMessageV2('demo', 'DemoAPI');
demotest.setQueryParameter('@param1', 'All systems');
demotest.setQueryParameter('@param2', 'action 3');
demotest.execute();

I'm getting an error saying invalid URI: https://www.demoendpoint.com/welcome?@param1=All+systems&@param2=action+3

You can see in the above URL space is replaced with + rather than %20

 

Can some please help?

 

Thanks,

Pradnyesh.

 

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@User477307 

did you try to encode the URI?

form the URL and then set the endpoint

like this

var url = ''; // form the url here
url = encodeURIComponent(url);

var demotest= new sn_ws.RESTMessageV2('demo', 'DemoAPI');
demotest.setEndpoint(url);
demotest.execute();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

Yes, I tried. But the problem now seems to be that

"space" is replaced with %20 and then %20 is replaced with %2520.

 

Also, upon further checking '/' in the url is also getting encoded.

We can't expect thrid party URL provider to change their query parameters.

 

Thanks,

Pradnyesh.

Hi @User477307 ,

 

Can you try in this way

var demotest = new sn_ws.RESTMessageV2('demo', 'DemoAPI');
demotest.setEndpoint('https://www.demoendpoint.com/welcome');
demotest.setHttpMethod('get');
demotest.setRequestHeader('Content-Type', 'application/json');
var param1 = 'All systems';
var param2 = 'action 3';
var url = 'https://www.demoendpoint.com/welcome?@param1=' + encodeURIComponent(param1) + '&@param2=' + encodeURIComponent(param2);
demotest.setEndpoint(url);
var response = demotest.execute();

 

Regards,

Shravan
please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi @Sai Shravan ,

Yes we tried but after setting the endpoint we are facing the same issue that space is converted to %20 in encodedURIComponent function and then in setEnpoint %20 is converted to %2520.

 

Still not found a workaround for this.

Any help would be appericiated.