Skip to lesson

Cookies: how they are set and sent

ShopLite wants to remember a visitor's theme and keep her signed in for a week. The server cannot write a note on her laptop by itself, so it must ask the browser to keep the note and bring it back. How does that note travel?

Set-Cookie and Cookie: the round trip

Set-Cookie is a response header with which a server creates a cookie: a name=value pair plus attributes. The client stores it and sends it back in the Cookie header.

Ask a test server for a cookie and read the Set-Cookie header in its response.

Example 1 · The server sets a cookie

Terminal
curl -i "https://httpbin.org/cookies/set?theme=dark"
Sample output
HTTP/1.1 302 FOUND
Date: Fri, 16 Oct 2026 02:43:22 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 223
Connection: keep-alive
Server: gunicorn/19.9.0
Location: /cookies
Set-Cookie: theme=dark; Path=/
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="/cookies">/cookies</a>.  If not click the link.

What this code does

  • httpbin.org is a free public service for testing HTTP. /cookies/set?theme=dark answers with Set-Cookie: theme=dark; Path=/ and a 302 redirect to /cookies.
  • One response can carry several Set-Cookie headers, one per cookie. Your curl may print HTTP/2 and lower-case header names; header names ignore case, so it means the same.

Example 2 · The client sends cookies back

Terminal
curl -b "theme=dark; lang=hi" https://httpbin.org/headers
Sample output
{
  "headers": {
    "Accept": "*/*", 
    "Cookie": "theme=dark; lang=hi", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/8.8.0", 
    "X-Amzn-Trace-Id": "Root=1-6ad18f4b-52eb1f681e0b6833750ba43f"
  }
}

What this code does

  • With text instead of a file name, -b sends that text as the Cookie header. /headers echoes back every header it received.
  • All cookies share one header, separated by ; . The server sees only names and values; it cannot tell when a cookie was set or which attributes it had.

The part before the first ; is the cookie itself. The rest are attributes: rules that tell the client how long to keep the cookie and where to send it.

On every later request to that site, the client sends its cookies back in one Cookie request header, such as Cookie: theme=dark; lang=hi. Only names and values travel back, never the attributes. A browser does this by itself; curl does it when you give it a cookie jar. Cookies are small, about 4 KB each, and sent on every matching request, so a cookie holds an id or a short setting, not a whole cart.

Lifetime: session cookies, Expires and Max-Age

Max-Age and Expires are cookie attributes that set how long the client keeps the cookie: Max-Age in seconds from now, Expires as a date. With neither, it is a session cookie and goes when the browser closes.

Save one cookie with no lifetime and one with a 7-day Max-Age in the same jar, then compare their expiry columns.

Example 3 · A session cookie and a 7-day cookie in one jar

Terminal
curl -L -c cookies.txt -b cookies.txt "https://httpbin.org/cookies/set?theme=dark"curl -c cookies.txt -b cookies.txt "https://httpbin.org/response-headers?Set-Cookie=remember=yes;%20Max-Age=604800;%20Path=/"cat cookies.txt
Sample output
$ curl -L -c cookies.txt -b cookies.txt "https://httpbin.org/cookies/set?theme=dark"
{
  "cookies": {
    "theme": "dark"
  }
}

$ curl -c cookies.txt -b cookies.txt "https://httpbin.org/response-headers?Set-Cookie=remember=yes;%20Max-Age=604800;%20Path=/"
{
  "Content-Length": "126", 
  "Content-Type": "application/json", 
  "Set-Cookie": "remember=yes; Max-Age=604800; Path=/"
}

$ cat cookies.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

httpbin.org	FALSE	/	FALSE	1792723406	remember	yes
httpbin.org	FALSE	/	FALSE	0	theme	dark

What this code does

  • -L follows the redirect, so the first command shows the cookies page. /response-headers returns any header you ask for, here a Set-Cookie with Max-Age=604800.
  • In the jar, the fifth column is the expiry as a Unix time (seconds since 1 January 1970). 0 marks the session cookie theme; remember expires 604800 seconds (7 days) after the request.

A cookie with Max-Age or Expires is a persistent cookie: it survives browser restarts until that time. Max-Age=604800 means 7 days from now, and if both are given, Max-Age wins. A cookie with neither is a session cookie, although some browsers bring it back when you restore old tabs.

To delete a cookie, the server sends it again with Max-Age=0 or an Expires date in the past.

Scope: Domain and Path

Domain and Path are cookie attributes that decide which requests carry the cookie. Domain names the site and its subdomains; Path limits the cookie to URLs that start with that path.

Set two cookies with different paths, then see which requests carry them.

Example 4 · Path decides which requests carry a cookie

Runs in: the demo login server on localhost:8080

Terminal
curl -c cookies.txt http://localhost:8080/prefscurl -b cookies.txt http://localhost:8080/echocurl -b cookies.txt http://localhost:8080/admin/echocat cookies.txt
Output
$ curl -c cookies.txt http://localhost:8080/prefs
{"message":"Preferences saved"}

$ curl -b cookies.txt http://localhost:8080/echo
{"path":"/echo","cookieHeader":"theme=dark"}

$ curl -b cookies.txt http://localhost:8080/admin/echo
{"path":"/admin/echo","cookieHeader":"adminTab=orders; theme=dark"}

$ cat cookies.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

localhost	FALSE	/admin	FALSE	0	adminTab	orders
localhost	FALSE	/	FALSE	0	theme	dark

What this code does

  • /prefs set two cookies: theme with Path=/ and adminTab with Path=/admin. The third column of the jar holds each path.
  • /echo received only theme; /admin/echo received both. Path keeps cookies tidy, but it is not a security wall: pages on the same host can still reach each other.

By default a cookie goes back only to the exact host that set it. Domain=college.edu widens it to that domain and every subdomain, such as portal.college.edu. A server can name only its own domain or a parent of it, never another site's.

Path=/admin sends the cookie only on requests whose path starts with /admin. Most login cookies use Path=/, so the whole site gets them.

Predict the output

Which requests carry the cookie?

Pick the Cookie header the request will carry.

Round 1 of 4

The request goes to a path inside /admin.

Set-Cookie: theme=dark; Path=/
Set-Cookie: adminTab=orders; Path=/admin
GET /admin/echo
Leave Domain out unless subdomains really need the cookie. A host-only cookie reaches fewer servers, which is the safer default.

Security attributes: HttpOnly, Secure and SameSite

HttpOnly is a cookie attribute that hides the cookie from JavaScript in the page, so a script injected by an attacker cannot read the login cookie.

Log in to the demo server and look at the Set-Cookie header and the saved jar.

Example 5 · A login cookie with every safety attribute

Runs in: the demo login server on localhost:8080

Terminal
curl -i -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8080/logincat cookies.txt
Sample output
$ curl -i -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8080/login
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 28
Connection: close
Date: Fri, 16 Oct 2026 02:38:47 GMT
Set-Cookie: SID=2c4b58b94fc7a948f5a1b41a64b01df3; Path=/; HttpOnly; Secure; SameSite=Lax

{"message":"Welcome, asha"}

$ cat cookies.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_localhost	FALSE	/	TRUE	0	SID	2c4b58b94fc7a948f5a1b41a64b01df3

What this code does

  • The jar marks an HttpOnly cookie with the #HttpOnly_ prefix, and the fourth column says TRUE for Secure. No lifetime was given, so the expiry column is 0: a session cookie.
  • The demo runs on plain http://localhost. Browsers and curl treat localhost as a safe exception and still accept Secure cookies there; on any other host, a Secure cookie needs HTTPS.

Two more attributes protect a login cookie. Secure makes the client send the cookie only over HTTPS. SameSite decides whether the cookie is sent on requests that another website starts:

  • SameSite=Strict: sent only when the request starts on the same site; even a link from another site arrives without it.
  • SameSite=Lax: also sent when the user clicks a normal link to your site, but not on forms or scripts from other sites. Chrome and Edge use Lax when no SameSite is given.
  • SameSite=None: sent on every cross-site request. Browsers reject it unless the cookie is also Secure.

A good login cookie looks like SID=...; Path=/; HttpOnly; Secure; SameSite=Lax, with no Domain and no long lifetime.

Fix the error

Fix the rejected cookie

The browser refuses the cookie. Pick the line that fixes it.

Round 1 of 2

The response is used by a page on another site. Fix the Set-Cookie line.

HTTP/1.1 200 OK
Content-Type: application/json
Set-Cookie: SID=b7f7eaee; Path=/; SameSite=None
{"message":"Welcome, asha"}

Error

This attempt to set a cookie via a Set-Cookie header was blocked because it had the "SameSite=None" attribute but did not have the "Secure" attribute, which is required in order to use "SameSite=None".

Seeing cookies in dev tools, and keeping them with curl

Run a full cookie-jar session: set two cookies, read the jar, then delete one of them.

Example 6 · A full cookie-jar session: set, read, delete

Terminal
curl -L -c cookies.txt -b cookies.txt "https://httpbin.org/cookies/set?theme=dark&lang=hi"cat cookies.txtcurl -L -c cookies.txt -b cookies.txt "https://httpbin.org/cookies/delete?theme"cat cookies.txt
Sample output
$ curl -L -c cookies.txt -b cookies.txt "https://httpbin.org/cookies/set?theme=dark&lang=hi"
{
  "cookies": {
    "lang": "hi", 
    "theme": "dark"
  }
}

$ cat cookies.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

httpbin.org	FALSE	/	FALSE	0	lang	hi
httpbin.org	FALSE	/	FALSE	0	theme	dark

$ curl -L -c cookies.txt -b cookies.txt "https://httpbin.org/cookies/delete?theme"
{
  "cookies": {
    "lang": "hi"
  }
}

$ cat cookies.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

httpbin.org	FALSE	/	FALSE	0	lang	hi

What this code does

  • To delete theme, httpbin sent Set-Cookie: theme=; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/. curl removed theme from the jar and kept lang, exactly as a browser does.
  • A jar file is as sensitive as a password: anyone who copies a jar with a login cookie can act as you. Delete jars when you finish, and never commit them to Git.

In Chrome or Edge, open dev tools (F12, or Cmd+Option+I on a Mac), go to the Application tab and open Storage → Cookies, then pick the site. Each row shows the name, value, domain, path, expiry, and the HttpOnly, Secure and SameSite settings. Firefox shows the same list under its Storage tab.

To watch cookies travel, open the Network tab, click a request and look at its Cookies tab, or at the Cookie and Set-Cookie lines under Headers. With curl, -c file saves cookies after a request and -b file sends them; use both on the same file to keep one jar up to date.

Next step

Check your understanding

11 quick questions