REST/Table API-Session Reuse for multiple request

bhavnagyanchand
Kilo Explorer

I am using REST/Table API for ServiceNow.

To process multiple requests , I am sending request to servicenow endpoint in a for each Loop.

I had a query here : In servicenow, for multiple requests, is the session maintained?

or Is there a provision using which   I can Reuse the connection with ServiceNow for multiple requests ?

14 REPLIES 14

floriandelmas7
Kilo Contributor

Hi Everyone,



Thank you so much everyone for your help !



So, to maintain a connectivity to realize multiple requests, the solution is to use the OAuth functionnality to obtain a token and use it after to each request.


The video present on this page (Enable OAuth with inbound REST) is very useful ! Thank you Anthony !



Compared to my question, regardless of the instance (local or remote), the solution is the same. Using the REST API to obtain data in JSON format (it's the same to obtain data in Excel or CSV file).



So, below, an example of REST API using the OAuth :


First, create a OAuth (Suystem OAuth > Application Registry > Create an OAuth API endpoint for external clients). For this test, define a password.


Second, use this OAuth in this script (usable in a background script) :


Replace the five first variables by your information.


var instance = 'url_of_your_instance'; // Example : https://dev01234.service-now.com


var username = 'admin';


var password = 'admin';


var clientId = 'client_id_of_your_oauth_record';


var clientSecret = 'client_secret_of_your_oauth_record';



// Request a token


var getTokenRM = new sn_ws.RESTMessageV2();


getTokenRM.setEndpoint(instance + '/oauth_token.do');


getTokenRM.setHttpMethod("post");


getTokenRM.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


getTokenRM.setRequestBody('grant_type=password&client_id=' + clientId + '&client_secret=' + clientSecret + '&username=' + username + '&password=' + password);


var responseToken = getTokenRM.execute();


var token = new JSONParser().parse(responseToken.getBody());



gs.print('Access token is : ' + token.access_token); // It's the token we will use during the next requests



// Write your differents requests with using the token obtain previously


var incidentRM = new sn_ws.RESTMessageV2();


incidentRM.setEndpoint(instance + '/api/now/table/incident?sysparm_limit=1');


incidentRM.setHttpMethod("get");


incidentRM.setRequestHeader("Accept", "application/json");


incidentRM.setRequestHeader("Authorization", "Bearer " + token.access_token);


var responseINC = incidentRM.execute();


gs.print(responseINC.getBody()); // An Incident record in JSON format



var problemRM = new sn_ws.RESTMessageV2();


problemRM.setEndpoint(instance + '/api/now/table/problem?sysparm_limit=1');


problemRM.setHttpMethod("get");


problemRM.setRequestHeader("Accept", "application/json");


problemRM.setRequestHeader("Authorization", "Bearer " + token.access_token);


var responsePRB = problemRM.execute();


gs.print(responsePRB.getBody()); // A Problem record in JSON format



// If you need to refresh token


var getTokenRM = new sn_ws.RESTMessageV2();


getTokenRM.setEndpoint(instance + '/oauth_token.do');


getTokenRM.setHttpMethod("post");


getTokenRM.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


getTokenRM.setRequestBody('grant_type=refresh_token&client_id=' + clientId + '&client_secret=' + clientSecret + '&refresh_token=' + token.refresh_token);


var responseToken = getTokenRM.execute();


token = new JSONParser().parse(responseToken.getBody());



var changeRM = new sn_ws.RESTMessageV2();


changeRM.setEndpoint(instance + '/api/now/table/change_request?sysparm_limit=1');


changeRM.setHttpMethod("get");


changeRM.setRequestHeader("Accept", "application/json");


changeRM.setRequestHeader("Authorization", "Bearer " + token.access_token);


var responseCHG = changeRM.execute();


gs.print(responseCHG.getBody()); // A Change record in JSON format



// Revoke the token to prevent attacks


var revokeTokenRM = new sn_ws.RESTMessageV2();


revokeTokenRM.setEndpoint(instance + '/oauth_token.do');


revokeTokenRM.setHttpMethod("post");


revokeTokenRM.setRequestHeader("Content-Type","application/x-www-form-urlencoded");


revokeTokenRM.setRequestBody('token=' + token.access_token);


getTokenRM.execute(); // Response is empty



I hope that it will help Bhavna. In any case, this will greatly help me !


Thanks again everyone for your time and your help,


Regards,


Florian.


Hi Florian Delmas,



The solution is to use the OAuth functionnality to obtain a token and use it after to each request.


Where the file is stored in servicenow instance like script. I need clarification for when this script file is running and stored.


And then we have credentials to access api then why do we need that access token.



Thanks,


Sakthivel.


Session RE-use is possible through the use of cookies. But, A cookie should be properly formatted in order to work.

I had a similar requirement. Please go through below article. 
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0657578

natalliar
ServiceNow Employee
ServiceNow Employee

You have to configure your HTTP client to maintain session cookie. If you are on Geneva or later in addition to that you would also have to send Authorization or X-User-Token (CSRF token) with each request.


Tushar Walveka2
Tera Guru

Hello @bhavnagyanchandani ,

Session RE-use is possible through the use of cookies. But, A cookie should be properly formatted in order to work.

I had a similar requirement. Please go through below article. 
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0657578