dreamworkAPI
Ctrl K
GitHub
classDocs / http / CookieJar

http.CookieJar

classsharedmenusource
Object → http.CookieJar

A cookie jar, stores cookies from Set-Cookie response headers and builds the Cookie request header, following RFC 6265.

Pass it as cookies to http.request to use it automatically.

Known limits:

  • the public suffix list is not checked, only bare top-level domains (Domain=com) are rejected, so a server can still set a cookie for a suffix like Domain=co.uk, keep jars per service you talk to;
  • reqwest passes only the last Set-Cookie header of a response, the others are lost;
  • cookies set by redirect responses are only stored with chttp, where redirects are followed in Lua, reqwest and HTTP follow redirects on their own and show only the final response;
  • cookies live in memory, use CookieJar:save and CookieJar:load to keep them between restarts.
lua
local jar = http.CookieJar()

http.request( { url = "https://example.com/login", method = "POST", cookies = jar } )
http.request( { url = "https://example.com/profile", cookies = jar } ) -- sends the session cookie

jar:set( "lang", "en", { domain = "example.com" } )
jar:get( "lang", "https://example.com/" ).value -- "en"

Constructor

http.CookieJar(): http.CookieJar

Fields

NameTypeDescription
[ integer ]http.CookieThe stored cookies, in creation order.

Methods

clearRemoves every cookie, or only the session ones (without expires).
deleteRemoves every cookie with the given name, only from domain if it is given.
getReturns the cookie with the given name that would be sent to url, the one with the longest path if there are several.
headerBuilds the Cookie request header for url, expired cookies are removed.
loadLoads cookies saved with CookieJar:save, replacing the ones with the same name, domain and path.
saveSaves the persistent cookies (the ones with expires) to dreamwork.storage.cookies under name, session cookies are skipp…
setSets a cookie, replacing the one with the same name, domain and path.
storeStores cookies from the Set-Cookie response header(s) of a response from url.

#CookieJar:clear

sharedmenusource
function http.CookieJar:clear(session_only?: boolean)

Removes every cookie, or only the session ones (without expires).

Parameters
NameTypeDescription
session_onlyoptionalbooleanIf true, only session cookies are removed.

#CookieJar:delete

sharedmenusource
function http.CookieJar:delete(name: string, domain?: string)

Removes every cookie with the given name, only from domain if it is given.

Parameters
NameTypeDescription
namestringThe cookie name.
domainoptionalstringThe domain to remove the cookie from.

#CookieJar:get

sharedmenunodiscardsource
function http.CookieJar:get(name: string, url?: ( string | URL )?): http.Cookie | nil

Returns the cookie with the given name that would be sent to url, the one with the longest path if there are several.

Without url the first not expired cookie with this name is returned.

Parameters
NameTypeDescription
namestringThe cookie name.
urloptional( string | URL )?The URL the cookie would be sent to.
Returns
TypeNameDescription
http.Cookie | nilcookieThe cookie, or nil if there is none.

#CookieJar:header

sharedmenunodiscardsource
function http.CookieJar:header(url: string | URL): string | nil

Builds the Cookie request header for url, expired cookies are removed.

Cookies with longer paths come first, see RFC 6265 5.4.

Parameters
NameTypeDescription
urlstring | URLThe URL the request is sent to.
Returns
TypeNameDescription
string | nilheaderThe header value, or nil if no cookies match.

#CookieJar:load

sharedmenusource
function http.CookieJar:load(name: string): integer

Loads cookies saved with CookieJar:save, replacing the ones with the same name, domain and path.

Expired cookies are skipped.

Parameters
NameTypeDescription
namestringThe name the jar was saved under.
Returns
TypeNameDescription
integercountThe number of loaded cookies, 0 if nothing was saved.

#CookieJar:save

sharedmenusource
function http.CookieJar:save(name: string)

Saves the persistent cookies (the ones with expires) to dreamwork.storage.cookies under name, session cookies are skipped, like a browser does on exit.

Saving a jar without persistent cookies removes the saved one.

Parameters
NameTypeDescription
namestringThe name to save the jar under.

#CookieJar:set

sharedmenusource
function http.CookieJar:set(name: string, value: string, options: http.CookieOptions): http.Cookie

Sets a cookie, replacing the one with the same name, domain and path.

An expired expires or a max_age of 0 or less removes the cookie, the returned cookie is not stored in that case.

Raises a TypeError on wrong argument types and a ValueError if name or value would break the Cookie header, or if neither domain nor a url with a host is given.

Parameters
NameTypeDescription
namestringThe cookie name, without "=", ";", ",", whitespace or control characters.
valuestringThe cookie value, sent as is, without ";" or control characters.
optionshttp.CookieOptionsThe cookie options, domain or url is required.
Returns
TypeNameDescription
http.CookiecookieThe cookie.

#CookieJar:store

sharedmenusource
function http.CookieJar:store(set_cookie: string | string[], url: string | URL)

Stores cookies from the Set-Cookie response header(s) of a response from url.

Values joined with "," by the http client are split back, invalid cookies and cookies for other domains are ignored.

Nothing is stored if url has no host (e.g. file:///).

Parameters
NameTypeDescription
set_cookiestring | string[]The Set-Cookie header value, or a list of values.
urlstring | URLThe URL of the response.
Generated from the dreamwork meta branch.