The Headers.getAll()
method seems to have provided a reliable way of parsing multivalued http headers. However, according to
documentation the method has been deprecated and removed from specification. There seems to be no method left that would support parsing multivalued headers. The Headers.get()
method seems to return an unparsed string containing several values. How do I reimplement the deprecated method in a reliable manner?
Asked
Active
Viewed 99 times
0

cyberixae
- 843
- 5
- 15
-
1It looks like the values simply get joined with comma-space in between, `, `. But to be able to simply split the value by that again, you would need to be sure, that none of the header values actually contains `, `. – CBroe Apr 06 '21 at 11:55
1 Answers
1
By looking at various sources I get the idea that comma is used as a separator and the values themselves can never contain commas within them. It also seems that whitespace before each individual value should be ignored but whitespace at the end of each value should perhaps preserved. Based on this I'm inclined to implement it as follows.
Headers.prototype.getAll = function(name) {
return this.get(name).split(',').map(v => v.trimStart());
}

cyberixae
- 843
- 5
- 15
-
I created a pull request for adding the polyfill to the documentation https://github.com/mdn/content/pull/3934 – cyberixae Apr 08 '21 at 09:50
-
1This doesn't work for `Set-Cookie`, because the values includes a comma. Example: `cookie1=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT, cookie2=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT, ` – Patrick Wozniak Dec 02 '22 at 10:48
-
1Cloudflare mentions this as well in their docs: https://developers.cloudflare.com/workers/runtime-apis/headers/#background – Patrick Wozniak Dec 02 '22 at 10:49