How to set Cookies received in response of Service CallOut Policy to request to Target Endpoint URL

Hi

I have a use case where I make a call to service using Service CallOut Policy and I receive some cookies in response and http status code as 200. So if http satus code is 200, then I need to pass these cookies as it is to my request to target endpoint URL so that we can get the cookies in target service.

If http status code is 401, we need to raise the fault saying that User is Unauthorized.

Best possible way to achieve this, any help would be highly appreciated

Thanks

0 7 5,240
7 REPLIES 7

adas
Participant V

@GAURAV I have attached a simple proxy bundle which illustrates the use of javascript to access the response headers from the service callout response and then setting a particular header to the target request.

You can add the javascript policy as a conditional policy based on the service callout response code. I have not implemented that part in the example.sc-headers-rev1-2015-12-29.zip

The javascript code for this is as follows:

var headerNames = context.getVariable("calloutResponse.headers.names") + "";
headerNames.split(", ").sort().forEach(function(headerName) {
    if (headerName)
        print(headerName+":"+context.getVariable("calloutResponse.header."+headerName));
    if (headerName.toLowerCase() == "etag")
      context.setVariable("request.header.cookie", context.getVariable("calloutResponse.header."+headerName));
});


This would print out all the header names and values from the service callout response object and additionally set the etag value from the service callout response header as the "cookie" in the target request header.

@arghya das

I have to access the response cookies not the headers from the service callout response and then setting these cookies to the target request.

In http, the cookies are sent using the Set-Cookie header so as long as you can access all the headers you can get the cookie and set it using the example I mentioned.

Alternately, you can also set the cookies in the http request object of javascript once you get them using the above snippet. There are examples available on the internet about setting cookies for a http request using JavaScript.

Hi @arghya das

I am getting response headers from Service Call out like below

[Set-Cookie: AUTHN_TOKEN=eyAidHlwIjogIkpXVCIsICJhbGciOiAiUlMyNTYiLCAiY3R5IjogIkpXVCIsICJraWQiOiAiMjk4MTNmNmYtNTEwNS00MTJkLTk5ODAtNzJhYTcyMGJlNzI0IiB9.eyAidG9rZW5OYW1lIjogImlkX3Rva2VuIiwgImF6cCI6ICJrcm9ub3MiLCAic3ViIjogIlN1cGVyVXNlciIsICJhdF9oYXNoIjogIi1JWDd3blRJX2VtV3Q5OV9WV0xFYnciLCAiaXNzIjogImh0dHA6Ly9hdXRoMi5mYWxjb24ua3Jvbm9zLmNvbTo4MC9vcGVuYW0vb2F1dGgyIiwgImlhdCI6IDE0NTE0NTEwMDUsICJhdXRoX3RpbWUiOiAxNDUxNDUxMDA1LCAiZXhwIjogMTQ1MTQ1MTYwNSwgInRva2VuVHlwZSI6ICJKV1RUb2tlbiIsICJyZWFsbSI6ICIvIiwgImF1ZCI6IFsgImtyb25vcyIgXSwgIm9wcyI6ICJmNWNC0xOTdmLTRlZjQtOTllYi0yZTc2ZWY3ZmQ1OWQiIH0.cctZGkZNsV5dKQTfndVhKwPnCKXmziYBi2aNO_gDQ_xFxgexkL8P2Tm53sF-aYU_XyerohWskMtknTjrAq4mANMgpgeMzi79FEzVBDaqLcvO6YYhx8IdS3pLhdAQwKk9h4lI3JGMtY8rlPW-6atkduh8sc1n-Ut6G4oBbxLnokFvpH7IT7mw3Jq1OMZVmFC2fZLPZMhD1MSqA1Mh_m3KBaxpaopqseC7eB7xpNOAl1JeJ3sHVnISQouUYN1E00Gi5_ZeXd8b02T0yq6DSywJ0ykmHwKw-gSNWhX6MyEg2DIBOqH4JVM9_uBciWsHpzH7oxf2pIvqfiIw3hsZwtbqwQ; Domain=.kronos.com; Path=/

, Set-Cookie: JWK_URI="http://test.domain.com/openam/oauth2/connect/jwk_uri"; Version=1; Domain=.test.com; Path=/

, Set-Cookie: amlbcookie=01; Domain=.test.com; Path=/

, Content-API-Version: protocol=1.0,resource=2.0 , Date: Wed, 30 Dec 2015 04:50:05 GMT

, Accept-Ranges: bytes

, Server: Restlet-Framework/2.1.7

, Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept

, Cache-Control: no-cache, no-store, must-revalidate

, Pragma: no-cache

, Expires: 0

, Content-Type: application/json;charset=UTF-8

, Content-Length: 139 ]

If you see it has multiple "Set-Cookie" headers but when I am trying to parse Cookie header in javascript and set it in request header to targetendpoint as you suggested, it sets only first "Set-Cookie" header and ignore the rest of "Set-Cookie" headers.

Is there a way, I can parse all the "Set-Cookie" headers coming in response from Service CallOut and set it in a request header cookie ?

Also is it possible to do this with any other available out of box policies without javascript policy?

var headerNames = context.getVariable("calloutResponse.headers.names") + "";

print("Header Names >>>>>>>>>>>> "+headerNames);

headerNames.split(", ").sort().forEach(function(headerName) {

if (headerName)

print(headerName+" : "+context.getVariable("calloutResponse.header."+headerName));

if (headerName == "Set-Cookie")

context.setVariable("request.header.cookie", context.getVariable("calloutResponse.header."+headerName));

});

risinha
Participant I

I am facing the same issue, can someone help ?