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,


This is how i have created rest message for google api.


If we go to the url :http://maps.googleapis.com/maps/api/geocode/json?address=india   ,we will get some json response.



To integrate this i have done the following steps




Screen Shot 2017-09-14 at 11.34.46 AM.png





Screen Shot 2017-09-14 at 11.34.30 AM.png




and i am using it as


var requestBody;


gs.print("start");


var responseBody;


var status;


var sm;


try{


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


gs.print("start1");


response = sm.execute();


gs.print("start2");


var jsonString = response.getBody();


gs.print(jsonString);


gs.print("start3");


var parsed = JSON.parse(jsonString);


gs.print("start4");


gs.print('result1' + parsed.result[0].field_name_1);


gs.print('result2' + parsed.result[0].field_name_2);



and response is null,


so error is


*** Script: start


*** Script: start1


java.lang.IllegalArgumentException: host parameter is null


*** Script: start2


*** Script:


*** Script: start3



Am i setting it wrongly (refer screenshots)?   or using it wrongly in script?


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Megha,


The endpoint for the REST message "google map" get is missing "https://"


If you add it and retest - all will be well!


Hi anthony ,


Thank you .It worked. now without any authentication it worked. I have to integrate some api , by authenticating   Oauth token. I have gone through the links that Shishir and harsh mentioned about Oauth profile.



Why should we do those steps cant we just set Auth token and get the response.?


Hi Megha,



if you want to use Oauth then you must be aware of your client id and client session and based on that it will generate a token.


i am not sure about https://google.map   . i had tested between two instance and i was able to achieve it.


i also tried with slack and service-now integration by using oauth then in that case i had used slack client session and slack client id to generate the token.