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

its okay Shishir, if you find anything that i am missing in both the script please let me know.


one more question. the script you had mentioned . is that working in background script?


no its not working


Can you please test is with basic authentication to see if you are getting result, please find the



var requestBody;


gs.print("start");


var responseBody;


var status;


var sm;


try{


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


gs.print("start1");


sm.setAuthenticationProfile('oauth2', 'token'); // have the name of the oauth2, if it's token then should be fine, more details is here: How to Setup OAuth2 authentication for outbound RESTMessageV2 integrations


gs.print("start2");


response = sm.execute();


var jsonString = response.getBody();  


var parsed = JSON.parse(jsonString);


gs.print('result1' + parsed.result[0].field_name_1); //get the field name which you want to print


gs.print('result2' + parsed.result[0].field_name_2); //get the field name which you want to print


responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();


status = response.getStatusCode();


} catch(ex) {


responseBody = ex.getMessage();


status = '500';


} finally {


requestBody = sm ? sm.getRequestBody():null;


}



i tested with below script with basic authentication and was able to get the result.


var r = new sn_ws.RESTMessageV2('2ndSNOWInstance', 'GET');


r.setStringParameter('number', 'INC0010448');


var response = r.execute();


var httpStatus = response.getStatusCode();


var parsed = JSON.parse(response.getBody());


gs.print('responseBody' + parsed.result[0].number);



find_real_file.png


Hi Shishir,


i am getting the output as


*** Script: start


*** Script: start1



and for this i just want to set token for Oauth, do i need to create new provider profile name. new or just setting token in this function call is enough   sm.setAuthenticationProfile('oauth2', 'token');


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