question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Response Headers]: set multiple headers of same key in response

See original GitHub issue

I’ve a node-proxy server using node-fetch to get the data from server. The server responds with multiple set-cookie header. I want to add this to the response to the client. I first parsed using res.headers.raw()['set-cookie'] this gives me array of cookies but now I want to set it in response and I tried using set, append both of these methods just set the value in one header set-cookie separated by comma. Is there any way to have multiple set-cookie headers in response? set-cookie: cookie1=value1 set-cookie: cookie2=value2

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
benjaminjkraftcommented, Jun 2, 2021

I want to highlight what @smolnikov said above – RFC 6265 says one SHOULD NOT do this for Set-Cookie specifically (and RFC 7230 echoes this as a special case). It’s acceptable for other headers, just not Set-Cookie. The browser fetch doesn’t need to care because it doesn’t let you see Set-Cookie at all.

That said, for anyone else reading this thread, it’s easy enough to work around by using headers.raw()['Set-Cookie'], which will give you a proper array of set-cookie values. (Except actually it’s a little more complicated than that, because you need to use the capitalization to match whatever the first time a set-cookie was added to this header. You can find this by iterating over headers.keys(), then using that name.) That’s what we’re doing.

2reactions
dzek69commented, Apr 14, 2020

Looks like this is how standard is defined, try this in the browser console and you will get the stringified result too:

x = new Response()
x.headers.set("a", "1")
x.headers.append("a", "2")
x.headers.get("a")

node-fetch is mostly about following the specs

What’s interesting is that documentation on MDN is wrong and suggests an array being returned when appending another header: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

console.log(myHeaders.get('X-Custom-Header')); // ['ProcessThisImmediately', 'AnotherValue']

But you can test yourself that’s not valid.

How to get around this? I have no idea. As you are controlling the response and not what’s reading it no “workaround” with splitting string is good here.

Probably using something else, more raw that is not following the Response/Headers standard is the only way to go.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting multiple response headers with same name, different ...
It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message ...
Read more >
How to add multiple Headers with the same name containing ...
The slice elements represent different values for the header named by the map key. Use Header.Add to add multiple values for a given...
Read more >
Multiple header values with the same name - HTTPie
If the request is sent with multiple headers that are sharing the same name, then the HTTPie will send them individually. It is...
Read more >
Access multi-value HTTP headers incorrectly in an API Proxy ...
Accessing the values of HTTP headers in Apigee policies in a way that returns only the first value is incorrect and can cause...
Read more >
HTTP headers - MDN Web Docs - Mozilla
HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found