set-cookie
header
Node.js 18 fetch and combined When fetching an endpoint which sets multiple cookies, the response has only a single combined set-cookie
header.
Example:
- add 2 cookies in endpoint
"set-cookie": [
"user=alice; Path=/; Expires=Thu, 01 Jun 2023 10:00:00 GMT; HttpOnly",
"role=guest; Path=/"
]
- fetch endpoint. response only has a single
set-cookie
header with values joined by,
set-cookie: user=alice; Path=/; Expires=Thu, 01 Jun 2023 10:00:00 GMT; HttpOnly, role=guest; Path=/
- a simple split by
,
is not enought as the cookie could contain additional,
characters (eg. in theexpires
value)
0: user=alice; Path=/; Expires=Thu
1: 01 Jun 2023 10:00:00 GMT; HttpOnly
2: role=guest; Path=/
set-cookie-parser
The library set-cookie-parser has a splitCookiesString(combinedSetCookieHeader)
function
references
- whatwg/fetch#973
- Wrong header split set-cookie node-fetch/node-fetch#349
- How to get an array of header values now that getAll has been removed? node-fetch/node-fetch#251
- https://stackoverflow.com/questions/63204093/how-to-get-set-multiple-set-cookie-headers-using-fetch-api
- https://nodejs.org/dist/latest-v18.x/docs/api/http.html#responsesetheadername-value