11cb0ef41Sopenharmony_ci# Cookie Handling 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci## `Cookie` interface 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci* **name** `string` 61cb0ef41Sopenharmony_ci* **value** `string` 71cb0ef41Sopenharmony_ci* **expires** `Date|number` (optional) 81cb0ef41Sopenharmony_ci* **maxAge** `number` (optional) 91cb0ef41Sopenharmony_ci* **domain** `string` (optional) 101cb0ef41Sopenharmony_ci* **path** `string` (optional) 111cb0ef41Sopenharmony_ci* **secure** `boolean` (optional) 121cb0ef41Sopenharmony_ci* **httpOnly** `boolean` (optional) 131cb0ef41Sopenharmony_ci* **sameSite** `'String'|'Lax'|'None'` (optional) 141cb0ef41Sopenharmony_ci* **unparsed** `string[]` (optional) Left over attributes that weren't parsed. 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci## `deleteCookie(headers, name[, attributes])` 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciSets the expiry time of the cookie to the unix epoch, causing browsers to delete it when received. 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci```js 211cb0ef41Sopenharmony_ciimport { deleteCookie, Headers } from 'undici' 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciconst headers = new Headers() 241cb0ef41Sopenharmony_cideleteCookie(headers, 'name') 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciconsole.log(headers.get('set-cookie')) // name=; Expires=Thu, 01 Jan 1970 00:00:00 GMT 271cb0ef41Sopenharmony_ci``` 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciArguments: 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci* **headers** `Headers` 321cb0ef41Sopenharmony_ci* **name** `string` 331cb0ef41Sopenharmony_ci* **attributes** `{ path?: string, domain?: string }` (optional) 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciReturns: `void` 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci## `getCookies(headers)` 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciParses the `Cookie` header and returns a list of attributes and values. 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci```js 421cb0ef41Sopenharmony_ciimport { getCookies, Headers } from 'undici' 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciconst headers = new Headers({ 451cb0ef41Sopenharmony_ci cookie: 'get=cookies; and=attributes' 461cb0ef41Sopenharmony_ci}) 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ciconsole.log(getCookies(headers)) // { get: 'cookies', and: 'attributes' } 491cb0ef41Sopenharmony_ci``` 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ciArguments: 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci* **headers** `Headers` 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ciReturns: `Record<string, string>` 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci## `getSetCookies(headers)` 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciParses all `Set-Cookie` headers. 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci```js 621cb0ef41Sopenharmony_ciimport { getSetCookies, Headers } from 'undici' 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ciconst headers = new Headers({ 'set-cookie': 'undici=getSetCookies; Secure' }) 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciconsole.log(getSetCookies(headers)) 671cb0ef41Sopenharmony_ci// [ 681cb0ef41Sopenharmony_ci// { 691cb0ef41Sopenharmony_ci// name: 'undici', 701cb0ef41Sopenharmony_ci// value: 'getSetCookies', 711cb0ef41Sopenharmony_ci// secure: true 721cb0ef41Sopenharmony_ci// } 731cb0ef41Sopenharmony_ci// ] 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci``` 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ciArguments: 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci* **headers** `Headers` 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ciReturns: `Cookie[]` 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci## `setCookie(headers, cookie)` 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ciAppends a cookie to the `Set-Cookie` header. 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci```js 881cb0ef41Sopenharmony_ciimport { setCookie, Headers } from 'undici' 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ciconst headers = new Headers() 911cb0ef41Sopenharmony_cisetCookie(headers, { name: 'undici', value: 'setCookie' }) 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ciconsole.log(headers.get('Set-Cookie')) // undici=setCookie 941cb0ef41Sopenharmony_ci``` 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ciArguments: 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci* **headers** `Headers` 991cb0ef41Sopenharmony_ci* **cookie** `Cookie` 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ciReturns: `void` 102