undici exports a set of cookie utility functions for working with HTTPDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/nodejs/undici/llms.txt
Use this file to discover all available pages before exploring further.
Cookie and Set-Cookie headers. These functions operate directly on undici’s Headers objects and follow the WHATWG cookie specification.
Importing
Cookie interface
All cookie functions work with theCookie object:
The cookie name.
The cookie value.
Expiry date or Unix timestamp in milliseconds.
Max-Age in seconds.
The domain attribute.
The path attribute.
Whether the Secure flag is set.
Whether the HttpOnly flag is set.
The SameSite attribute value.
Left-over attributes that were not parsed.
getCookies(headers)
Parses the Cookie request header and returns a key-value map of all cookie name/value pairs.
A
Headers instance containing the Cookie header.Record<string, string>
Parsing request cookies
getSetCookies(headers)
Parses all Set-Cookie response headers and returns an array of Cookie objects.
A
Headers instance containing one or more Set-Cookie headers.Cookie[]
Parsing Set-Cookie response headers
setCookie(headers, cookie)
Appends a Set-Cookie header to a Headers instance.
A
Headers instance to append the cookie to.The cookie to set.
void
Setting cookies on a response
deleteCookie(headers, name[, attributes])
Sets a cookie’s expiry to the Unix epoch (January 1, 1970), signaling browsers to delete it.
A
Headers instance to append the delete instruction to.The name of the cookie to delete.
Optional path and domain to scope the deletion.
void
Deleting a cookie
parseCookie(cookieString)
Parses a raw cookie string into a Cookie object.
A raw
Set-Cookie header value string.Cookie
Working with fetch responses
UsegetSetCookies with fetch responses to inspect cookies returned by the server:
Inspecting cookies from a fetch response