REST from script include

maneesh3
Tera Contributor

Hi,

How to call REST api resource path (GET) from script include to trigger REST method.

Please help in this

 

 

Thanks a ton!

 

1 ACCEPTED SOLUTION

Nootan
ServiceNow Employee
ServiceNow Employee

Hi Maneesh,

For this, You can use this code.

var RESOURCE_PATH = 'api/..etc'
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://INSTANCE.service-now.com/'+RESOURCE_PATH );
request.setHttpMethod('GET');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';
request.setBasicAuth(user,password);

//If having any requestBody
request.setRequestBody(JSON.stringify(requestBody));

request.setRequestHeader("Accept","application/json");
var response = request.execute();
gs.log(response.getBody());

Thanks

View solution in original post

10 REPLIES 10

Hey,

Please go through the link may be this is helpful to you

https://community.servicenow.com/community?id=community_question&sys_id=4a5e26bedbe76b444abd5583ca96...

 

Please mark correct or helpful if this solves your issue.

Thanks

Swapnil

Nootan
ServiceNow Employee
ServiceNow Employee

Hi Maneesh,

You can try this code.

 

try { 
 var r = new sn_ws.RESTMessageV2(REST Message, REST Method);

//override authentication profile 
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);

//Append a parameter to the end of the request URL with the form name=v­alue.
r.setQue­ryP­ara­met­er(­name, value)

//Set a REST message function variable with the specified name from the REST message record to the specified value.
r.setStr­ing­Par­ame­ter­(na­me,­value)

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}​

Thanks.

maneesh3
Tera Contributor

Hi Nootan,

 

Thanks a lot for reply. 

How can I call REST resource path (GET) method instead of message name.

new sn_ws.RESTMessageV2(REST Message, REST Method);

Nootan
ServiceNow Employee
ServiceNow Employee

Hi Maneesh,

Can you share the REST resource path to which you are referring to ? 

Thanks

maneesh3
Tera Contributor

Hi Nootan,

 

Please find below as reference:

kindly help me in this for resource path

 

find_real_file.png