docs.rodeo

MDN Web Docs mirror

Set-Cookie header

The HTTP Set-Cookie {{Glossary("response header")}}  is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.

[!WARNING] Browsers block frontend JavaScript code from accessing the Set-Cookie header, as required by the Fetch spec, which defines Set-Cookie as a forbidden response header name that must be filtered out from any response exposed to frontend code.

When a Fetch API or XMLHttpRequest API request uses CORS, browsers will ignore Set-Cookie headers present in the server’s response unless the request includes credentials. Visit Using the Fetch API - Including credentials and the XMLHttpRequest article to learn how to include credentials.

For more information, see the guide on Using HTTP cookies.

Header type `{{Glossary("Response header")}}` 
`{{Glossary("Forbidden request header")}}`  No
`{{Glossary("Forbidden response header name")}}`  Yes

Syntax

Set-Cookie: <cookie-name>=<cookie-value>
Set-Cookie: <cookie-name>=<cookie-value>; Domain=<domain-value>
Set-Cookie: <cookie-name>=<cookie-value>; Expires=<date>
Set-Cookie: <cookie-name>=<cookie-value>; HttpOnly
Set-Cookie: <cookie-name>=<cookie-value>; Max-Age=<number>
Set-Cookie: <cookie-name>=<cookie-value>; Partitioned
Set-Cookie: <cookie-name>=<cookie-value>; Path=<path-value>
Set-Cookie: <cookie-name>=<cookie-value>; Secure

Set-Cookie: <cookie-name>=<cookie-value>; SameSite=Strict
Set-Cookie: <cookie-name>=<cookie-value>; SameSite=Lax
Set-Cookie: <cookie-name>=<cookie-value>; SameSite=None; Secure

// Multiple attributes are also possible, for example:
Set-Cookie: <cookie-name>=<cookie-value>; Domain=<domain-value>; Secure; HttpOnly

Attributes

Some cookie names contain prefixes that impose specific restrictions on the cookie’s attributes in supporting user-agents. All cookie prefixes start with a double-underscore (__) and end in a dash (-). The following prefixes are defined:

[!WARNING] You cannot count on these additional assurances on browsers that don’t support cookie prefixes; in such cases, prefixed cookies will always be accepted.

Examples

Session cookies are removed when the client shuts down. Cookies are session cookies if they do not specify the Expires or Max-Age attribute.

Set-Cookie: sessionId=38afes7a8

Permanent cookies are removed at a specific date (Expires) or after a specific length of time (Max-Age) and not when the client is closed.

Set-Cookie: id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT
Set-Cookie: id=a3fWa; Max-Age=2592000

Invalid domains

A cookie for a domain that does not include the server that set it should be rejected by the user agent.

The following cookie will be rejected if set by a server hosted on original-company.com:

Set-Cookie: qwerty=219ffwef9w0f; Domain=some-company.co.uk

A cookie for a subdomain of the serving domain will be rejected.

The following cookie will be rejected if set by a server hosted on example.com:

Set-Cookie: sessionId=e8bb43229de9; Domain=foo.example.com

Cookie names prefixed with __Secure- or __Host- can be used only if they are set with the Secure attribute from a secure (HTTPS) origin.

Cookie names prefixed with __Http- or __Host-Http- can be used only if they are set with the Secure attribute from a secure (HTTPS) origin and in addition must have the HttpOnly attribute set to prove that they were set via the Set-Cookie header and not on the client-side via JavaScript.

In addition, cookies with the __Host- or __Host-Http- prefix must have a path of / (meaning any path at the host) and must not have a Domain attribute.

// Both accepted when from a secure origin (HTTPS)
Set-Cookie: __Secure-ID=123; Secure; Domain=example.com
Set-Cookie: __Host-ID=123; Secure; Path=/

// Rejected due to missing Secure attribute
Set-Cookie: __Secure-id=1

// Rejected due to the missing Path=/ attribute
Set-Cookie: __Host-id=1; Secure

// Rejected due to setting a Domain
Set-Cookie: __Host-id=1; Secure; Path=/; Domain=example.com

// Only settable via Set-Cookie
Set-Cookie: __Http-ID=123; Secure; Domain=example.com
Set-Cookie: __Host-Http-ID=123; Secure; Path=/
Set-Cookie: __Host-example=34d8g; SameSite=None; Secure; Path=/; Partitioned;

[!NOTE] Partitioned cookies must be set with Secure. In addition, it is recommended to use a __Host or __Host-Http- prefix when setting partitioned cookies to make them bound to the hostname and not the registrable domain.

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN