3 ms·
I love fetch, except for the way that it combines headers. If more than one of the same header is set (such as multiple set-cookie headers), it combines them al
by nfriedly 5y ago
I love fetch, except for the way that it combines headers. If more than one of the same header is set (such as multiple set-cookie headers), it combines them all into a single comma-separated string. I know this is allowed in the HTTP spec, and it's probably even a sensible default. But the fetch spec doesn't allow any access to the raw headers, so there's no straightforward way to get the original uncombined headers back. Set-Cookie headers, in particular, often contain commas in their expiration dates, so just splitting around `, ` will lead to problems.
I took a look through the source of this new fetch API, and it seems to have inherited that wart:
https://github.com/nodejs/undici/blob/2dd3437e20c5a3cc466226c544f048351baffc78/lib/fetch/headers.js#L103 https://github.com/nodejs/undici/blob/2dd3437e20c5a3cc466226...
I've argued with the authors of the fetch spec about this before, and ultimately settled on using https://www.npmjs.com/package/set-cookie-parser#user-content-splitcookiesstringcombinedsetcookieheader https://www.npmjs.com/package/set-cookie-parser#user-content... to work around this flaw. (For clarity, I published the package, but chrusart wrote that method - https://github.com/nfriedly/set-cookie-parser/pull/19 https://github.com/nfriedly/set-cookie-parser/pull/19)