I am running into issues when trying to add and modify set-cookie headers in the response from the target server before returning to the client. I am ending up with a single set-cookie header with a comma separated list of set-cookie headers. How do I set the cookies? (part 2)
- GET COOKIES: It took me a while to figure out how to read in and process multiple set-cookie headers using various community questions. Some worked, some did not for me.
if (context.getVariable('response.header.set-cookie.values.count')) {
var numHeaders = response.headers['Set-Cookie'].length();
var allCookies = [];
for (var i=0; i<numHeaders; ++i) {
var cookie = response.headers['Set-Cookie'][i];
var cookie2 = !!cookie.match(/Expires=/i) ? response.headers['Set-Cookie'][++i] : null;
var fullCookie = cookie + (cookie2 ? ', ' + cookie2 : '');
// do stuff to the full cookie
allCookies.push(fullCookie.replace(/\s+/, ' '));
}
}
allCookies.push('<new cookie>');
allCookies.push('<new cookie>);
- SET COOKIES: How do I set the cookies? I have added and modified to the response so they each end up as a separate header. Everything I have tried ends up with a single set-cookie header will them comma separated.
I have tried things like:
context.setVariable(‘response.header.set-cookie’, allCookies.join(‘,’));
context.setVariable(‘response.header.set-cookie’, allCookies);
I have tried setting each one separately in a for loop, and again by adding .0, .1, …, .index to response.header.set-cookie.
I have also tried modifying without setVariable too: response.headers[‘Set-Cookie’][i] = allCookies[i];
Any help would be appreciated. I am sure I could do this with normal AssignMessage policies that clear then add, but I am not familiar enough with those either to know how to deal with a list of items to set.