Skip to the workspace
Pebellslearn by building
HTTP in Depth  ›  Lesson

Headers and Cookies

HTTP Headers 12 minHeaders carry information about a message, such as its format, credentials and caching rules, and cookies are headers a browser stores and sends back automatically.

api design / Headers and Cookies

Explore each scene at your own pace.

01 · The rule

The rule

Headers and Cookies

The body of a BookNook response holds the book. Everything about the exchange lives elsewhere: who is asking, what format was sent, how long it may be cached, when to try again. That information travels in headers, and cookies are simply a pair of headers with a special job.

The rule

The body is the letter; headers are the envelope.

A header is one line: a case-insensitive name, a colon, a value. Useful groups:

  • Describing the body: Content-Type, Content-Length, Content-Encoding.
  • Negotiation: Accept, Accept-Language.
  • Identity: Authorization carries credentials, like Bearer eyJhbGci....
  • Caching: Cache-Control, ETag, If-None-Match.
  • Control: Location after a create, Retry-After after a 429 or 503.
  • Tracing: a request id such as X-Request-Id that you can search for in logs. The X- prefix is an old habit; new standard headers no longer use it.

Cookies let a server store a small value in a browser. The server sends Set-Cookie; from then on, the browser automatically attaches it to matching requests in a Cookie header:

HTTP/1.1 200 OK
Set-Cookie: session=a91f3c; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=3600

GET /v1/members/me/loans HTTP/1.1
Cookie: session=a91f3c

The attributes are where the safety lives:

  • HttpOnly: page JavaScript cannot read the cookie, so a script injected into the page cannot steal it.
  • Secure: sent only over HTTPS.
  • SameSite: limits sending the cookie on requests started by other sites, which blunts cross-site request forgery.
  • Max-Age or Expires: when the browser should throw it away.

Cookies are a browser feature. A mobile app or a server calling BookNook usually sends an Authorization header instead.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.