Skip to main content

Documentation 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.

undici exports a set of cookie utility functions for working with HTTP Cookie and Set-Cookie headers. These functions operate directly on undici’s Headers objects and follow the WHATWG cookie specification.

Importing

import { getCookies, getSetCookies, setCookie, deleteCookie, parseCookie } from 'undici'
All cookie functions work with the Cookie object:
name
string
The cookie name.
value
string
The cookie value.
expires
Date | number
Expiry date or Unix timestamp in milliseconds.
maxAge
number
Max-Age in seconds.
domain
string
The domain attribute.
path
string
The path attribute.
secure
boolean
Whether the Secure flag is set.
httpOnly
boolean
Whether the HttpOnly flag is set.
sameSite
'Strict' | 'Lax' | 'None'
The SameSite attribute value.
unparsed
string[]
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.
headers
Headers
required
A Headers instance containing the Cookie header.
Returns: Record<string, string>
Parsing request cookies
import { getCookies, Headers } from 'undici'

const headers = new Headers({
  cookie: 'session=abc123; user=alice; theme=dark'
})

const cookies = getCookies(headers)
console.log(cookies)
// { session: 'abc123', user: 'alice', theme: 'dark' }

console.log(cookies.session) // 'abc123'

getSetCookies(headers)

Parses all Set-Cookie response headers and returns an array of Cookie objects.
headers
Headers
required
A Headers instance containing one or more Set-Cookie headers.
Returns: Cookie[]
Parsing Set-Cookie response headers
import { getSetCookies, Headers } from 'undici'

const headers = new Headers({
  'set-cookie': 'undici=getSetCookies; Secure; HttpOnly; SameSite=Strict'
})

const cookies = getSetCookies(headers)
console.log(cookies)
// [
//   {
//     name: 'undici',
//     value: 'getSetCookies',
//     secure: true,
//     httpOnly: true,
//     sameSite: 'Strict'
//   }
// ]
Appends a Set-Cookie header to a Headers instance.
headers
Headers
required
A Headers instance to append the cookie to.
The cookie to set.
Returns: void
Setting cookies on a response
import { setCookie, Headers } from 'undici'

const headers = new Headers()

setCookie(headers, {
  name: 'session',
  value: 'abc123',
  httpOnly: true,
  secure: true,
  sameSite: 'Lax',
  path: '/',
  maxAge: 3600
})

console.log(headers.get('set-cookie'))
// 'session=abc123; Path=/; Max-Age=3600; Secure; HttpOnly; SameSite=Lax'

deleteCookie(headers, name[, attributes])

Sets a cookie’s expiry to the Unix epoch (January 1, 1970), signaling browsers to delete it.
headers
Headers
required
A Headers instance to append the delete instruction to.
name
string
required
The name of the cookie to delete.
attributes
object
Optional path and domain to scope the deletion.
Returns: void
Deleting a cookie
import { deleteCookie, Headers } from 'undici'

const headers = new Headers()
deleteCookie(headers, 'session')

console.log(headers.get('set-cookie'))
// 'session=; Expires=Thu, 01 Jan 1970 00:00:00 GMT'

// With path and domain
deleteCookie(headers, 'user', { path: '/app', domain: 'example.com' })

parseCookie(cookieString)

Parses a raw cookie string into a Cookie object.
A raw Set-Cookie header value string.
Returns: Cookie
import { parseCookie } from 'undici'

const cookie = parseCookie('session=abc123; Path=/; HttpOnly; Secure; SameSite=Lax')
console.log(cookie.name)    // 'session'
console.log(cookie.value)   // 'abc123'
console.log(cookie.httpOnly) // true

Working with fetch responses

Use getSetCookies with fetch responses to inspect cookies returned by the server:
Inspecting cookies from a fetch response
import { fetch, getSetCookies } from 'undici'

const response = await fetch('https://api.example.com/login', {
  method: 'POST',
  body: JSON.stringify({ username: 'alice', password: 'secret' }),
  headers: { 'content-type': 'application/json' }
})

const cookies = getSetCookies(response.headers)
const sessionCookie = cookies.find(c => c.name === 'session')
console.log(sessionCookie?.value)

Build docs developers (and LLMs) love