Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Parsing callOut response cookie and passing to backend API

Not applicable

We are currently developing an API proxy that uses a 3rd party API. The problem is, the 3rd party API requires use of cookies (which we know is not restful... but have no choice to use it).

We are currently hitting their API with our authentication info using a callOut through JavaScript. We then add the access token that is received back to the targetRequest header (v1.0, will introduce cache later on) and submit the request.

However, in order to make the call, I also need to pass the cookie that is received from the callOut. The cookie is received through the 'Set-Cookie' header that is received with the access token.

Without using/developing a Node.js app, is there a way to parse the cookie that is sent to us in order to insert it into the targetRequest header (under a 'Cookie' header).

We know that node has a 'cookie-parser' module, though this is not available without using a Node.js app.

We also cannot rely on cookie being passed back to the client, stored in the browser and then resubmitted.

Any help is greatly appreciated

Solved Solved
0 3 2,209
1 ACCEPTED SOLUTION

Aha, still doable. Here's a js policy (no error handling, etc.) that makes a request to https://www.google.com and sets a context variable to the content of the 'S' portion of the returned cookies. It also uses the print function in js policy so when you're tracing the execution you'll see debug statements in the "output from all transactions" section of the trace.

var goog = httpClient.get('https://www.google.com')
goog.waitForComplete()
var resp = goog.getResponse()
if (resp.status = 200) {
  var cookie = resp.headers['Set-Cookie']
  context.setVariable('sCookie', /S=(.*);/.exec(cookie)[1])
}
print('status ' + resp.status)
print('set-cookie ' + resp.headers['Set-Cookie'])
print('regex ' + /S=(.*);/.exec(cookie))

View solution in original post

3 REPLIES 3