How can I set multiple cookies on a request in a JavaScript callout?

Not applicable

I see many answers on to how to RETRIEVE multiple cookies from a RESPONSE. However, when somebody asks how to SET multiple cookies on a REQUEST, all I hear are crickets in the background...

I tried the following:

var headers = {
//some headers here...

// and now the cookies...
"set-cookie": cookies[0]+"; "+cookies[1]
}

...where I got the cookies from a previous response like so:

var cookies = []
cookies.push(response.getResponse().headers["set-cookie"][0])
cookies.push(response.getResponse().headers["set-cookie"][1])

This didn't work.

I then tried:

var headers = {
//some other headers here...

// and now the cookies...
"set-cookie": cookies
}

...and still nothing...

Does someone have an example of how to get this working?

Solved Solved
0 2 18.5K
1 ACCEPTED SOLUTION

Not applicable

I've got it working! First of all, setting a cookie on a request should be done to the "cookie" header, like so:

headers = {
// some other headers here
"cookie": cookies[0]+"; "+cookies[1]
};

Second, the original cookies from the previous response looked like this:

cookie1=value; path=/; domain=<the domain>; Expires=<the expiry date>;
cookie2=value; path=/; domain=<the domain>; Expires=<the expirey date>;

...and sending all of it is wrong.

I just need the first parts of the cookies:

cookie1=value
cookie2=value

...now when the target receives the cookie header it looks like this:

cookie: "cookie1=value; cookie2=value"

...and the target server is happy with it.

View solution in original post

2 REPLIES 2

Not applicable

I've got it working! First of all, setting a cookie on a request should be done to the "cookie" header, like so:

headers = {
// some other headers here
"cookie": cookies[0]+"; "+cookies[1]
};

Second, the original cookies from the previous response looked like this:

cookie1=value; path=/; domain=<the domain>; Expires=<the expiry date>;
cookie2=value; path=/; domain=<the domain>; Expires=<the expirey date>;

...and sending all of it is wrong.

I just need the first parts of the cookies:

cookie1=value
cookie2=value

...now when the target receives the cookie header it looks like this:

cookie: "cookie1=value; cookie2=value"

...and the target server is happy with it.

Thank you! Helpful.