- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2017 10:44 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2017 02:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2017 12:40 AM
its okay Shishir, if you find anything that i am missing in both the script please let me know.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2017 12:42 AM
one more question. the script you had mentioned . is that working in background script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2017 02:51 AM
no its not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2017 01:15 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2017 03:06 AM
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');
