Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Getting REST response as null , for the same api curl command is working properly.

meg6
Kilo Expert

Hi ,

I am getting the response when i use curl command    

curl -H 'Accept: application/json' -H 'Content-Type:application/json' -XGET https://someapi.example -H 'authorization:token' , for this I am getting the response.

But when i use it in javascript as following,

var r = new RESTMessage('https://someapi.example', 'get');

r.setStringParameter('token', 'token');

var response = r.execute();

var jsonString = response.getBody();  

var parser = new JSONParser();  

var parsed = parser.parse(jsonString);  

getting error as:org.mozilla.javascript.EcmaError: Cannot convert null to an object.

am i missing anything?

The response here is null.

1 ACCEPTED SOLUTION

Hey,


This worked for me,




var requestBody;


gs.print("start");


var responseBody;


var status;


var sm;


try{


sm = new sn_ws.RESTMessageV2("test", "get");


gs.print("start1");


sm.setRequestHeader("Authorization","token-string");


response = sm.execute();


gs.print("start2");


var jsonString = response.getBody();



Thank you all for your response



View solution in original post

32 REPLIES 32

Hi Harsh,


I am not using https://google.map   , I was just trying an example api (to check how it works)   . I am not understanding why we need to use two instances , i want to get some data from an api, for that i need to pass(set) Authorization token(which i have already).



like how curl command works.


curl -H 'Accept: application/json' -H 'Content-Type:application/json' -XGET https://someapi.example -H 'authorization:token_id'


Hey,


This worked for me,




var requestBody;


gs.print("start");


var responseBody;


var status;


var sm;


try{


sm = new sn_ws.RESTMessageV2("test", "get");


gs.print("start1");


sm.setRequestHeader("Authorization","token-string");


response = sm.execute();


gs.print("start2");


var jsonString = response.getBody();



Thank you all for your response



I had the same issue using Rest Outbound service. I was able to solve it by prefixing https:// to end the point. I changed at the Rest Message End Point as well as at the Get Method End Point. 

Please mark the answer helpful if it helps you.