Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

can you please check the below url. we are missing something



https://codegists.com/code/servicenow%20sample%20http%20request/


I have one doubt , I am running it in background script so that if it works i can use it in workflow activity. Is it correct place?


which workflow activity are you using ?



i think yes but i never tried on workflow activity.



REST Message workflow activity


I think this script looks correct ,but getting the response as null.


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);


can you please try with below script and see what are you getting the output in script include?



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


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


var response = r.execute();


var jsonString = response.getBody();


var httpStatus = response.getStatusCode();


gs.print('responseBody' + responseBody);


gs.print('httpStatus' + httpStatus);


//var parser = new JSONParser();


//var parsed = parser.parse(jsonString);