Skip to content

HTTP Headers, Cookies and Caching: Cache-Control, ETag, 304 and Set-Cookie with Real Examples

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

The status code says what happened; the headers say everything else. They tell the server which format and language you want, tell the browser how long it may keep a copy, carry cookies that keep you logged in, and switch on security rules. In this lesson you send custom headers from your browser and see exactly what arrived, read the caching headers of a real API and of needmytools.com, save 83 bytes with a real 304 Not Modified, watch a server set two cookies and send them back, and find out why JavaScript could not read a header that curl showed plainly.

Quick answer

Headers are name: value lines on requests and responses. Key request headers are Accept, Accept-Language, Content-Type for the body you send, Authorization and Cookie. Key response headers are Content-Type, Cache-Control (how long a copy may be reused), ETag (a version tag for conditional requests that can return 304 Not Modified), Location and Set-Cookie. Mark login cookies Secure, HttpOnly and SameSite=Lax or stricter.

40 min read and practice · Content checked on 29 November 2026

In this lesson you will

  • Send and read request and response headers with curl and fetch
  • Read Cache-Control and use ETag for a 304 Not Modified response
  • Explain how Set-Cookie and Cookie work and set safe cookie flags

Request headers: telling the server about you

httpbin’s /headers address replies with the request headers it received, which makes it a mirror. From curl we sent Accept: application/json and Accept-Language: ar-AE, and the mirror showed those two plus Host and curl’s User-Agent. Accept asks for a format, Accept-Language asks for a language, so a well built API can answer Arabic text to a user in Dubai without a separate address. Content-Type is the matching header when you send a body: it says what the body is, not what you want back.

The browser sends far more by itself. Our live example asked for JSON and added a custom X-Shop header, and the browser added Origin, Sec-Fetch-* hints that describe the request, client hints such as Sec-Ch-Ua-Platform, and its own User-Agent. Origin: null appears because the example runs in a sandboxed frame with no real origin. Some headers, such as Host, Origin and Cookie, are forbidden for JavaScript to set, so a page cannot pretend to be another site.

Try it: What your browser really sends
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

In headless Chrome 153 on Linux the list had 12 headers, including "Origin": "null", "Sec-Fetch-Mode": "cors" and our "X-Shop": "souq-demo". Your list will differ by browser, system and language settings, which is the point: run it and compare.

Example: curl and a header mirror
Real output: curl https://httpbin.org/headers -H "Accept-Language: ar-AE" -H "Accept: application/json"
{
  "headers": {
    "Accept": "application/json",
    "Accept-Language": "ar-AE",
    "Host": "httpbin.org",
    "User-Agent": "curl/8.5.0"
  }
}

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

What is the difference between no-cache and no-store?

no-cache allows storing a copy but requires checking with the server before using it, often with an ETag and a cheap 304. no-store forbids keeping any copy at all. Despite the name, no-cache does cache.

Are cookies and localStorage the same thing?

No. Cookies are sent to the server automatically with every matching request and can be HttpOnly. localStorage stays in the browser, is never sent by itself and is always readable by scripts, so it is a poor place for login tokens.

How big can a cookie be?

Browsers guarantee about 4 KB per cookie and limit how many each site may keep. Every cookie is sent with every request to that site, so keep them few and small: store an ID in the cookie and the data on the server.

Why do I still see an old version of a page after an update?

Some cache along the way, the browser or a CDN, kept a copy that its Cache-Control still allows. A hard reload skips the browser cache; a CDN needs a purge or a shorter max-age. Versioned file names such as app.3f9a.js avoid the problem for static files.

Can I make up my own HTTP headers?

Yes, any name works, such as X-Shop in the example. The old X- prefix is no longer required. From a browser, a custom request header triggers a CORS preflight, so the server must allow it in Access-Control-Allow-Headers.

Sources

Facts in this lesson were checked against these official pages on 29 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.