How can i get and set cookie parameter in REST Message?

Rimi
Kilo Contributor

How can i get and set cookie parameter in REST Message? i have tried to use getCookies(), setCookies(), getHeader() but not able to get response properly... on trying getHeader("set-cookie") it gives the complete header value and i am not able to set it.

13 REPLIES 13

Rimi
Kilo Contributor

yes i am able to get cookie value but i need to set some unique value in cookie param of Header when i call the next REST Message in order to link two web service call... addHeader("set-cookie", "xyz Value"); this is the syntax i cud find in wiki but it doesn't help and i am still receiving a concurrency issue.


I guess am not able to explain the scenario very clearly...


iaind
Kilo Contributor

Not sure it's exactly the same scenario but this is what I've got working (just).   The downside is I've only managed to get this working in our Fuji instance.   I can't seem to access the response header data prior to Fuji...



We have an app which requires a auth request to get a token and cookie.   The token and cookie is then passed to all subsequent requests to authenticate.


I get the key and cookie using this:


              sApiKey = json.apiKey;


              aHeaders       = response.getHeaders();


              for ( row in aHeaders ) { if ( row == 'Set-Cookie' ) { sCookie = getCookie(aHeaders[row]); } }



function getCookie(inString) {


      inString = inString.split(';');


      return inString[0];


}



I can then set the auth token and cookie using this:


      rest.setRequestHeader('Cookie', inCookie);


      rest.setRequestHeader('Authorization', inToken);


iaind
Kilo Contributor

Too eager last time.   This does work in Eureka.


1 - I use two REST Messages.   The first gets the auth token and the cookie.


2 - The auth token and cookie are passed as tokens in "REST Message Function Headers" for "Cookie" and "Authorization"


Iain,



I'm trying to do the same thing but I'm unable to recreate what you did.   Do you still have this example on how to use cookies for REST authentication?


iaind
Kilo Contributor

Hello, we didn't continue with this route since the target started supporting oauth.   I've tried to re-create an example from memory.   Hopefully it helps.   It gets NID then attempts to set it in the next request (I know it's a bit duff, but I couldn't think of a site to do this with right now):



var sNid = '';


sNid = getHttpNid(sNid);


gs.print(sNid);


sNid = getHttpNid(sNid);


gs.print(sNid);



function getHttpNid(inNid) {


      var oResponse, oHeaders, oCookies;


      var nStatusCode = 500, sResponseBody, sRequestBody;


      var sNid;



      var rest = new sn_ws.RESTMessageV2();


      rest.setHttpMethod('GET');


      rest.setEndpoint('http://www.google.co.uk');


      if ( JSUtil.notNil(inNid) ) { rest.setRequestHeader('NID', inNid); }


      rest.setRequestHeader('cache-control', 'no-cache');



      try {


              oResponse         = rest.execute();


              nStatusCode     = oResponse.getStatusCode();


              sResponseBody = oResponse.getBody();


              oHeaders           = oResponse.getHeaders();


              sCookies           = oResponse.getRequestHeader('Set-Cookie');


      } catch ( err ) {


              sResponseBody = err.getMessage();


      } finally {


              sRequestBody   = oResponse.getRequestBody();


      }


      if ( JSUtil.nil(oCookies) ) { for ( row in oHeaders ) { if ( row == 'Set-Cookie' ) { oCookies = oHeaders[row].toString(); } } }


      oCookies = oCookies.split('; ');


      for ( var i = 0; i < oCookies.length; i++ ) { if ( oCookies[i].indexOf('NID=') >= 0 ) { sNid = oCookies[i].replace(/NID=/, ''); break; } }


      return sNid;


}