tough-cookie.cookiejar.setcookie_2.md
July 9, 2025 ยท View on GitHub
Home > tough-cookie > CookieJar > setCookie
CookieJar.setCookie() method
Attempt to set the Cookie in the CookieJar.
Signature:
setCookie(cookie: string | Cookie, url: string | URL, options?: SetCookieOptions): Promise<Cookie | undefined>;
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
cookie |
string | Cookie |
The cookie object or cookie string to store. A string value will be parsed into a cookie using Cookie.parse(). |
|
url |
string | URL |
The domain to store the cookie with. |
|
options |
(Optional) Configuration settings to use when storing the cookie. |
Returns:
Promise<Cookie | undefined>
Remarks
-
If successfully persisted, the Cookie will have updated Cookie.creation, Cookie.lastAccessed and Cookie.hostOnly properties.
-
As per the RFC, the Cookie.hostOnly flag is set if there was no
Domain={value}attribute on the cookie string. The Cookie.domain property is set to the fully-qualified hostname ofcurrentUrlin this case. Matching this cookie requires an exact hostname match (not a domainMatch() as per usual)