On this page

  • name string
  • value string
  • expires Date|number (optional)
  • maxAge number (optional)
  • domain string (optional)
  • path string (optional)
  • secure boolean (optional)
  • httpOnly boolean (optional)
  • sameSite 'Strict'|'Lax'|'None' (optional)
  • unparsed string[] (optional) Left over attributes that weren't parsed.
deleteCookie(headers, name, attributes?): void

Sets the expiry time of the cookie to the unix epoch, causing browsers to delete it when received.

import { deleteCookie, Headers } from 'undici'

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

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

Arguments:

  • headers Headers
  • name string
  • attributes { path?: string, domain?: string } (optional)

Returns: void

getCookies(headers): void

Parses the Cookie header and returns a list of attributes and values.

import { getCookies, Headers } from 'undici'

const headers = new Headers({
  cookie: 'get=cookies; and=attributes'
})

console.log(getCookies(headers)) // { get: 'cookies', and: 'attributes' }

Arguments:

  • headers Headers

Returns: Record<string, string>

getSetCookies(headers): void

Parses all Set-Cookie headers.

import { getSetCookies, Headers } from 'undici'

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

console.log(getSetCookies(headers))
// [
//   {
//     name: 'undici',
//     value: 'getSetCookies',
//     secure: true
//   }
// ]

Arguments:

  • headers Headers

Returns: Cookie[]

parseCookie(cookie): void

Parses a single Set-Cookie header value into a Cookie object.

import { parseCookie } from 'undici'

console.log(parseCookie('undici=getSetCookies; Secure; SameSite=Lax'))
// {
//   name: 'undici',
//   value: 'getSetCookies',
//   secure: true,
//   sameSite: 'Lax'
// }

Notes:

  • The cookie value is returned as it appears in the header. Percent-encoded sequences such as %20 or %0D%0A are not decoded.
  • sameSite is only set for exact case-insensitive matches of Strict, Lax, or None.

Arguments:

  • cookie string

Returns: Cookie | null

setCookie(headers, cookie): void

Appends a cookie to the Set-Cookie header.

import { setCookie, Headers } from 'undici'

const headers = new Headers()
setCookie(headers, { name: 'undici', value: 'setCookie' })

console.log(headers.get('Set-Cookie')) // undici=setCookie

Arguments:

  • headers Headers
  • cookie Cookie

Returns: void