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

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Maneesh,

 

You can use the REST API Explorer to build your REST request. The related links near the send button will give you code snippets for integrating to the ServiceNow APIs in several commonly used languages. More info here.

https://developer.servicenow.com/dev.do#!/learn/learning-plans/kingston/servicenow_application_devel...

-Pradeep Sharma

Hi Pradeep,

 

Actually Iam working on web phone integration so I need to call scripted REST api (resource path) from script include to trigger REST and add info message

please help me with the code .

Swapnil Soni1
Giga Guru

Hi maneesh,

You don't need to include it.   When you create your Script Include, it is loaded into the accessible script cache.   From inside your Scripted Rest API, you can directly call the Script Include (as long as it is Active) in this way:

Script Include Name: MyScriptInclude

If your API is in the same scope as your Script Include:

var myinclude = new MyScriptInclude();

If it is in a different scope:

var myinclude = new script_include_scope_prefix.MyScriptInclude();

 

Or You can use it-

Script include GET example

This example demonstrates how to use a script include to provide a response. By using a script include you can reuse common code and maintain readability in the REST service scripts.

/**
 * GET - Sample Request API - Script Include
 */
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var responseObj = global.SampleDataUtil.getSampleJSON();
    return responseObj;
})(request, response);

 

Please mark correct or helpful if this solves your issue.

Thanks

Swapnil

Hi soni,

Thanks for your fast response.Actually Iam working on web phone integration so I need to call scripted REST api (resource path) from script include to trigger REST and add info message

please help me with the code .