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.

How to get/set cookies for REST API

simonezini
Giga Sage

Hi all,

I'm trying to call a REST API and it needs two different step: the first one to get an access token, the second one to do the REST call.

I'm able to do the first one, in fact I'm getting the access token correctly.

The problems come with the second one, because I need to get cookies and token from the first call and pass them to the 2nd.

 

I'm able to pass the access token in header with setRequestHeader(string Name, string Value), but I can't reach to correctly set cookies in the 2nd REST call. Here the code used for the 2nd call:

//"token" contains token from the first call

var r = new sn_ws.RESTMessageV2('Create user', 'POST');
var body = '';
r.setRequestHeader('X-CSRF-Token', token.toString());

/***** BODY START *****/
body += '{\n';
body += '......';
body += '}\n';
/***** BODY END *****/

r.setRequestBody(body);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

 

However I don't know how to use setCookies() or a similar function to correctly pass the cookies when doing POST REST call.

Could please anyone help me?

 

Thanks!

Simone

1 ACCEPTED SOLUTION

simonezini
Giga Sage

Hi all,

in the end we manage to remove the access token from the third-party application and chanage auth type for the 2nd call (without an access token).

 

Thanks for your replies

View solution in original post

5 REPLIES 5

simonezini
Giga Sage

Hi all,

in the end we manage to remove the access token from the third-party application and chanage auth type for the 2nd call (without an access token).

 

Thanks for your replies