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.

Issue with body of rest API script

Snehal13
Kilo Sage

Getting error as 

Javascript compiler exception: unterminated string literal 

 

Script is below

 

 

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('<apiURLhere>');
request.setHttpMethod('POST');
 
var payload="{
    'data': [
        '<value here>'
    ]
}";

request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');

request.setRequestBody(payload);
var response = request.execute();

gs.log(response.getBody());

 

 

 

What is breaking the body part ?

3 REPLIES 3

Ivan Betev
Mega Sage
Mega Sage

Hello @Snehal13 ,

 

try this:

 

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('<apiURLhere>');
request.setHttpMethod('POST');
 
var payload = '{
    "data": [
        "<value here>"
    ]
}';

request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Content-Type", "application/json");

request.setRequestBody(payload);
var response = request.execute();

gs.log(response.getBody());

 

 

however, I'd configure the message first through Rest Message module:

IvanBetev_0-1707216467151.png

and then I'd call it like prescribed in this reference to the API: sn_ws.RESTMessageV2 

Regards, Ivan

SunilKumar_P
Giga Sage

Hi @Snehal13, This occurs due to the unterminated string literal somewhere in the code, as you have already tried with double quotes for paylaod variable object, can you check with back tick (``) or single quote ('')

 

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('<apiURLhere>');
request.setHttpMethod('POST');
 
var payload = `{
    "data": [
        "<value here>"
    ]
}`;

request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');

request.setRequestBody(payload);
var response = request.execute();

gs.log(response.getBody());

OR

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('<apiURLhere>');
request.setHttpMethod('POST');
 
var payload = '{
    "data": [
        "<value here>"
    ]
}';

request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');

request.setRequestBody(payload);
var response = request.execute();

gs.log(response.getBody());

 

Regards,

Sunil

Harish KM
Kilo Patron
Kilo Patron

Hi @Snehal13 remove the "" double codes after payload, that should solve the issue.

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('<apiURLhere>');
request.setHttpMethod('POST');
var payload={
'data': [
'<value here>'
]
};
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');

request.setRequestBody(payload);
var response = request.execute();

gs.log(response.getBody());

Regards
Harish