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

for now i am not requesting any token , i have   the token


setAuthenticationProfile('oauth2', '<pass oauth profile sys id >');


Hi Harsha,


setAuthenticationProfile('oauth2', '<pass oauth profile sys id >');


for this i should create oauth2 record in table , but where should i specify the token .I don't know what to fill in that form, mainly provider and where will token comes into picture.


I think that is for get token. can you explain me clearly



Screen Shot 2017-09-07 at 4.04.01 PM.png


Hi Megha,



have you checked this blog ?



How to setup OAuth2 authentication for RESTMessageV2 integrations



if you want to build token based integration then you must need to create application registry and then here you can select that in oauth provider field.



Set the credentials for the REST message using an existing basic auth or OAuth 2.0 profile.


NameTypeDescription
typeStringThe type of authentication profile to use. Valid values are 'basic' to use basic authentication, or 'oauth2' to use OAuth 2.0.
profileIdStringThe sys_id of an authentication profile record. When using basic auth, specify the sys_id of a Basic Auth Configuration [sys_auth_profile_basic] record. When using OAuth 2.0, specify the sys_id of a OAuth Entity Profile [oauth_entity_profile] record.


RESTMessageV2 - setAuthenticationProfile(String type, String profileId)


Hi Shishir and Harsh,


I went through the   link you both suggested.To set the third party Oauth provider we should have client id and secret.In that link they have created those in different instance as Oauth for consumer, and using it in different instance as provider.



In my case I just have an API and I need to get some data, by providing token.


I have also follow the steps as they have done it for Yahoo finance in service now, but its not working.



And my first script also



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.



Is the script incorrect?


Is there a way that i can use curl command directly?