Getting "Error executing REST request: Invalid uri" from API response

pkarki113
Mega Contributor

So I am trying to make a GET request in my business rule. However, when I debug it via application logs I am getting "Error executing REST request: Invalid uri". When I paste the URL, my browser can load it and I get a response from the API. What could be going wrong?

My code is here: https://pastebin.com/xExKD3s5

I googled my error, but the only ServiceNow specific question I found did not seem to pertain to programatically performed HTTP requests, instead it was done using some sort of interface for making HTTP requests.

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

What is the endpoint URL? Is that onPremise or on cloud. If end point is on-premise, you also need a mid Server.

 

Also can you change this line to 

var url = 'http://apilayer.net/api/validate?access_key=a3b93a330639ef3615d23d20b489d38c&address1='+ add+'&postal_code=' + zipp + '&locality=' + city + '&country_code=USA';

gs.info('+++url is++++'+url);

request.setEndpoint(url);


Please mark this response as correct or helpful if it assisted you with your question.

I did as you asked. The logs say this: 

+++url is++++http://apilayer.net/api/validate?access_key=a3b93a330639ef3615d23d20b489d38c&address1=1600 Pennsylvania Ave SE&postal_code=20003&locality=Washington&country_code=USA

http response error: Error invoking http request: Error executing REST request: Invalid uri 'http://apilayer.net/api/validate?access_key=a3b93a330639ef3615d23d20b489d38c&address1=1600 Pennsylvania Ave SE&postal_code=20003&locality=Washington&country_code=USA': Invalid query

Once again, if I copy and paste the URL, I get a successful response. As far as if the API is on-premise or cloud...I'm not sure. https://streetlayer.com/documentation That is their website, but I didn't see anything on their main docs page.

 

Rashmi Bansal
Mega Guru

Hi,

 

Instead of passing values in endpoint, set all with the help of setQueryParameter(parametername,value);

like your url will be

 

var endpoint = "http://apilayer.net/api/validate?";
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(endpoint);
request.setHttpMethod('GET');
request.setRequestHeader("Accept","application/json");
request.setQueryParameter('access_key','a3b93a330639ef3615d23d20b489d38c&address1=');
request.setQueryParameter('postal_code',value);

 

Thanks,

Rashmi