• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Cookie Handling
2
3## `Cookie` interface
4
5* **name** `string`
6* **value** `string`
7* **expires** `Date|number` (optional)
8* **maxAge** `number` (optional)
9* **domain** `string` (optional)
10* **path** `string` (optional)
11* **secure** `boolean` (optional)
12* **httpOnly** `boolean` (optional)
13* **sameSite** `'String'|'Lax'|'None'` (optional)
14* **unparsed** `string[]` (optional) Left over attributes that weren't parsed.
15
16## `deleteCookie(headers, name[, attributes])`
17
18Sets the expiry time of the cookie to the unix epoch, causing browsers to delete it when received.
19
20```js
21import { deleteCookie, Headers } from 'undici'
22
23const headers = new Headers()
24deleteCookie(headers, 'name')
25
26console.log(headers.get('set-cookie')) // name=; Expires=Thu, 01 Jan 1970 00:00:00 GMT
27```
28
29Arguments:
30
31* **headers** `Headers`
32* **name** `string`
33* **attributes** `{ path?: string, domain?: string }` (optional)
34
35Returns: `void`
36
37## `getCookies(headers)`
38
39Parses the `Cookie` header and returns a list of attributes and values.
40
41```js
42import { getCookies, Headers } from 'undici'
43
44const headers = new Headers({
45  cookie: 'get=cookies; and=attributes'
46})
47
48console.log(getCookies(headers)) // { get: 'cookies', and: 'attributes' }
49```
50
51Arguments:
52
53* **headers** `Headers`
54
55Returns: `Record<string, string>`
56
57## `getSetCookies(headers)`
58
59Parses all `Set-Cookie` headers.
60
61```js
62import { getSetCookies, Headers } from 'undici'
63
64const headers = new Headers({ 'set-cookie': 'undici=getSetCookies; Secure' })
65
66console.log(getSetCookies(headers))
67// [
68//   {
69//     name: 'undici',
70//     value: 'getSetCookies',
71//     secure: true
72//   }
73// ]
74
75```
76
77Arguments:
78
79* **headers** `Headers`
80
81Returns: `Cookie[]`
82
83## `setCookie(headers, cookie)`
84
85Appends a cookie to the `Set-Cookie` header.
86
87```js
88import { setCookie, Headers } from 'undici'
89
90const headers = new Headers()
91setCookie(headers, { name: 'undici', value: 'setCookie' })
92
93console.log(headers.get('Set-Cookie')) // undici=setCookie
94```
95
96Arguments:
97
98* **headers** `Headers`
99* **cookie** `Cookie`
100
101Returns: `void`
102