<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question How to get/set cookies for REST API in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2142998#M799924</link>
    <description>&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;I'm able to do the first one, in fact I'm getting the access token correctly.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm able to pass the access token in header with&amp;nbsp;&lt;EM&gt;setRequestHeader(string Name, string Value),&amp;nbsp;&lt;/EM&gt;but I can't reach to correctly set cookies in the&amp;nbsp;2nd REST call. Here the code used for the 2nd call:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;//"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();
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However I don't know how to use&amp;nbsp;&lt;EM&gt;setCookies()&amp;nbsp;&lt;/EM&gt;or a similar function to correctly pass the cookies when doing POST REST call.&lt;/P&gt;
&lt;P&gt;Could please anyone help me?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;Simone&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 21 Jan 2021 17:15:53 GMT</pubDate>
    <dc:creator>simonezini</dc:creator>
    <dc:date>2021-01-21T17:15:53Z</dc:date>
    <item>
      <title>How to get/set cookies for REST API</title>
      <link>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2142998#M799924</link>
      <description>&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;I'm able to do the first one, in fact I'm getting the access token correctly.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm able to pass the access token in header with&amp;nbsp;&lt;EM&gt;setRequestHeader(string Name, string Value),&amp;nbsp;&lt;/EM&gt;but I can't reach to correctly set cookies in the&amp;nbsp;2nd REST call. Here the code used for the 2nd call:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;//"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();
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However I don't know how to use&amp;nbsp;&lt;EM&gt;setCookies()&amp;nbsp;&lt;/EM&gt;or a similar function to correctly pass the cookies when doing POST REST call.&lt;/P&gt;
&lt;P&gt;Could please anyone help me?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;Simone&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 17:15:53 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2142998#M799924</guid>
      <dc:creator>simonezini</dc:creator>
      <dc:date>2021-01-21T17:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to get/set cookies for REST API</title>
      <link>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2142999#M799925</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;SPAN class="ng-binding"&gt;simonezini,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I am facing a similar challenge.&amp;nbsp; Maybe if we pool our experience, we can find the solution.&amp;nbsp; Based on your notes, it sounds like you are familiar with&amp;nbsp;response.getCookies();&amp;nbsp; to pull out the cookies from the first call.&amp;nbsp; You can also use response.getHeader('Set-Cookie'); which gives the cookies in a slightly different format.&lt;/P&gt;
&lt;P&gt;For the second call, I have used:&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt; r.setRequestHeader('Cookie',cookie);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This mimics my Postman layout, but does not appear to be received by the Endpoint.&amp;nbsp; At the end of the day, a cookie is simply a custom Header to pass across.&amp;nbsp; I believe the Host isn't aligned for the cookie to validate, or I have entered some value wrong.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, you could add a variable and pass it in.&amp;nbsp; Let me know if it works better for you:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;r.setStringParameterNoEscape('cookie',cookie);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;Edit: I figured it out!!!!!!!!!!!&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;// Original header
var r = new sn_ws.RESTMessageV2('Create user', 'POST');
var body = '';
r.setRequestHeader('X-CSRF-Token', token.toString());
// Adding in the cookies
var cookies = response.getCookies(); // This is the response from the GET token call.
var cookieResponse = processCookies(cookies);  // This is how you transform the cookie to the right format.
r.setRequestHeader("Cookie",cookieResponse); // This is how you add the cookies to the POST.

function processCookies(cookiesSource){
   // The response is neither an array or a string, so I bullied it into an array.
   var cookies = cookiesSource.toString();
   cookies = cookies.replace("[", "");
   cookies = cookies.replace("]","");
   var arrayCookie = cookies.split(',');
   
   var response = ""; 
   for (var i = 0; i&amp;lt;arrayCookie.length; i++){
      //for each cookie provided, you need to parse it down to "JSESSIONID=EB233D63974F.........3DE1ECD1F02;" and combine it
      var start = cookie.split(';');
      response += start[0] + ";";
   }
   gs.info("Response from arrayCookie: "+ response);
   return response;
}&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps!&lt;/P&gt;
&lt;/DIV&gt;</description>
      <pubDate>Wed, 17 Feb 2021 17:30:23 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2142999#M799925</guid>
      <dc:creator>howardaustin</dc:creator>
      <dc:date>2021-02-17T17:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to get/set cookies for REST API</title>
      <link>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2143000#M799926</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am not getting even the access token(token in your code).My requirement is the same of yours.Can i get your 1st code which is used to get the x-csrf&amp;nbsp; token.if resolved can i get the second code.i tried a lot to figure it out but couldn't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Sowmya&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 07:00:34 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2143000#M799926</guid>
      <dc:creator>Sowmya Chinnara</dc:creator>
      <dc:date>2021-05-18T07:00:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to get/set cookies for REST API</title>
      <link>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2143001#M799927</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;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).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your replies&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 15:53:59 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2143001#M799927</guid>
      <dc:creator>simonezini</dc:creator>
      <dc:date>2021-05-18T15:53:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to get/set cookies for REST API</title>
      <link>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2143002#M799928</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;here's the code I've used to get the first access token (X-CSRF-TOKEN):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var token = '';
try{
	var r = new sn_ws.RESTMessageV2('TEST - Token', 'GET');
	
	var response = r.execute();
	var responseBody = response.getBody();
	var httpStatus = response.getStatusCode();
	gs.log("REST Call - StatusCode: " + httpStatus );
	gs.log("REST Call - ResponseBody : " + responseBody);
	
	if(httpStatus == 200 || httpStatus == 201){
		token = response.getHeader("X-CSRF-Token");
		gs.log("REST Call - Token : " + token);
	}
} catch(ex) {
	var message = ex.getMessage();
}&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 May 2021 15:57:54 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/2143002#M799928</guid>
      <dc:creator>simonezini</dc:creator>
      <dc:date>2021-05-18T15:57:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to get/set cookies for REST API</title>
      <link>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/3404541#M1232942</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;In the code sample above, the array is produced using string replacement and split.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;   // The response is neither an array or a string, so I bullied it into an array.
   var cookies = cookiesSource.toString();
   cookies = cookies.replace("[", "");
   cookies = cookies.replace("]","");
   var arrayCookie = cookies.split(',');&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;In fact, the method&amp;nbsp;RESTMessageV2.getCookies() returns an java ArrayList.&lt;/P&gt;&lt;P&gt;The best way to proceed is to use the native Java iterator, more reliable:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// The response is a native Java ArrayList, so you can prefer to build an Javascript Array
var iterator = cookiesSource.iterator();
var arrayCookie = [];
while (iterator.hasNext()) {
   arrayCookie.push(iterator.next());
}&lt;/LI-CODE&gt;&lt;P&gt;I hope it can help.&lt;/P&gt;&lt;P&gt;Thank you for the reply, it was very useful for me too!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Loïc&lt;/P&gt;</description>
      <pubDate>Tue, 14 Oct 2025 08:39:38 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/how-to-get-set-cookies-for-rest-api/m-p/3404541#M1232942</guid>
      <dc:creator>loicthery</dc:creator>
      <dc:date>2025-10-14T08:39:38Z</dc:date>
    </item>
  </channel>
</rss>

