Need to get/set cookie parameters in REST message

Neha15
Kilo Contributor

Hi All,

I am getting the session id within the cookies when I tested the REST API in postman tool. I have configured the REST messages in ServiceNow and calling them from a scheduled script. I need to pass the session id from one request to the subsequent requests. Can anyone please let me know how this can be done using the script.

Thanks in Advance.

 

 

5 REPLIES 5

Mike Bahr
Giga Contributor
I believe getCookies() should work for this.

Neha15
Kilo Contributor

Hi Mike,

I tried that, but not getting any response.

See my code below.

var cookies = responseBody.getCookies();
var i;
for(i=0;i<cookies.size();i++) {
gs.print(‘cookie: ‘ + cookies.get(i));
}

plaura
Tera Contributor

Hi Neha,

 

You find Cookie outside response.

try:

var cookies = response.getCookies();
for( var i=0; i < cookies.size(); i++ ){
  gs.log('cookie: ' + cookies.get(i));
}

 

(Use all response instead of only responseBody).

indhudurai
Kilo Contributor

Hi Neha

 

Did it work? I am trying to get the Cookie token from SOAP-WSDL and pass it to REST call for authentication. It is a 2 step process too, hence wondering how it worked for you. 

Thanks!