Web·5 min read·By the developer of StackUtils

HTTP Headers Every Developer Should Know

HTTP headers control caching, security, CORS, content negotiation, and more. This guide covers the 20 most important headers with real-world examples and the common mistakes that cause bugs in production.

What Headers Are, on the Wire

HTTP headers are name–value pairs sent before the body of every request and response. Names are case-insensitive; HTTP/2 and HTTP/3 transmit them in lowercase, which is why DevTools may show content-type where your code wrote Content-Type. A real exchange, as printed by curl -i, looks like this:

GET /api/users/42 HTTP/1.1
Host: api.example.com
Accept: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9…

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: private, max-age=0, must-revalidate
ETag: "u42-v7"
Vary: Authorization

{"id":42,"name":"Alice"}

To see what a live site actually sends, run curl -sI https://example.com or paste the URL into the HTTP headers checker, which also follows the redirect chain and flags missing security headers.

Request Headers

AuthorizationBearer eyJhbGci…

Credentials: a bearer token (often a JWT), Basic (Base64 of user:password — encoded, not encrypted) or a custom scheme.

Content-Typeapplication/json

Format of the request body. Servers commonly reject or misparse a JSON body sent without it.

Acceptapplication/json

Formats the client can handle. The server picks one and should answer 406 if it cannot satisfy any.

Cookiesession=abc123; theme=dark

Cookies the browser stored for this site, sent automatically subject to their SameSite, Path and Domain attributes.

Originhttps://app.example.com

Scheme + host + port of the page that made the request. Browsers send it on cross-origin and on most POST requests; CORS decisions are based on it.

If-None-Match"33a64df5"

The ETag of a cached copy. If it still matches, the server answers 304 Not Modified with no body.

Response Headers

Content-Typetext/html; charset=utf-8

Format of the body. Declare charset for text/* types; application/json is UTF-8 by definition and has no charset parameter.

Cache-Controlpublic, max-age=3600

Who may cache the response and for how long. Detailed below.

ETag"33a64df5"

Opaque version identifier of the representation, used for conditional requests.

Location/users/42

Target of a 3xx redirect, or the URL of the resource created by a 201.

Set-Cookiesid=…; HttpOnly; Secure; SameSite=Lax; Path=/

Stores a cookie. One header per cookie — they cannot be comma-joined like other headers.

VaryAccept-Encoding, Origin

Request headers that change the response. Caches store one copy per combination.

Cache-Control in Practice

Cache-Control is where most production header bugs live. The two directives people confuse most: no-cache means “you may store it, but ask the server before reusing it”; no-store means “do not store it at all”.

DirectiveMeaning
max-age=NFresh for N seconds after the response was generated
s-maxage=NOverrides max-age for shared caches (CDNs, proxies) only
no-cacheStore, but revalidate before every reuse
no-storeNever write to any cache
privateOnly the user's browser may store it, not a CDN
publicShared caches may store it, even for authenticated requests
must-revalidateOnce stale, never serve without revalidating
immutableWill not change while fresh; skip revalidation on reload
stale-while-revalidate=NMay serve stale for N seconds while refreshing in the background

Policies that work for the usual kinds of response:

# Fingerprinted assets: app.3f9a2c.js, logo.81bd.svg
Cache-Control: public, max-age=31536000, immutable

# HTML documents: always check for a new deploy, use ETag for cheap 304s
Cache-Control: no-cache

# Personalised API responses
Cache-Control: private, no-cache

# Bank statements, tokens, anything that must not hit disk
Cache-Control: no-store

The classic mistake is a long max-age on a URL whose content changes, such as /app.js without a hash in its name. Browsers keep the old file until it expires and there is no way to recall it. Long lifetimes are only safe when the URL changes with the content.

Worked Example: Revalidation with ETag

With Cache-Control: no-cache and an ETag, the second visit costs one round trip and no body:

# First request
GET /index.html           →  200 OK
                              ETag: "v15"
                              Cache-Control: no-cache
                              (12 KB body)

# Later: browser revalidates
GET /index.html
If-None-Match: "v15"      →  304 Not Modified
                              ETag: "v15"
                              (no body, cached copy reused)

If revalidation never yields a 304, check that the ETag is stable across servers. Load balancers in front of several machines that each generate their own ETag (for example from file inode) return a different value on every request, so the cache never matches.

CORS Headers and the Preflight

CORS is enforced by the browser, not the server: the request usually reaches your API, and the browser then decides whether the page may read the response. For a request with a JSON body or an Authorization header, the browser first sends a preflight:

OPTIONS /api/orders
Origin: https://app.example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: authorization, content-type

204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: authorization, content-type
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 600
Vary: Origin

When CORS fails, the console message tells you which rule broke. The frequent ones:

  • Wildcard with credentials. Access-Control-Allow-Origin: * is rejected when the request includes cookies (credentials: 'include'). Echo the specific allowed origin instead, and add Vary: Origin so a CDN does not serve one origin's answer to another.
  • Preflight returns 401 or 404. The OPTIONS request carries no credentials. Auth middleware must let it through, and the router must have an OPTIONS handler.
  • Custom header not listed. Every non-simple request header, such as X-Request-Id, must appear in Access-Control-Allow-Headers.
  • Response header invisible to JavaScript. Beyond a short safelist, response headers are hidden unless named in Access-Control-Expose-Headers.

Browsers cap Access-Control-Max-Age (Chromium at two hours), so very large values give no extra benefit.

A Security Header Baseline

A reasonable starting set for an HTML site served over HTTPS:

Strict-Transport-Security: max-age=31536000; includeSubDomains
Content-Security-Policy: default-src 'self'; frame-ancestors 'none'; object-src 'none'; base-uri 'self'
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
  • HSTS makes the browser refuse plain HTTP for the given period. Enable includeSubDomains only when every subdomain serves HTTPS; adding preload and submitting to the preload list is hard to undo.
  • CSP is the strongest XSS mitigation, and the most likely to break a site. Deploy it first as Content-Security-Policy-Report-Only, read the violation reports, then enforce.
  • frame-ancestors in CSP replaces X-Frame-Options for clickjacking protection. Sending X-Frame-Options: DENY as well is harmless for older browsers. Note that frame-ancestors is ignored in a <meta> CSP; it only works as a header.
  • Cookies: session cookies should be HttpOnly; Secure; SameSite=Lax (or Strict). SameSite=None requires Secure. The __Host- name prefix additionally forces Secure, Path=/ and no Domain attribute.

Headers to Stop Sending

HeaderStatusDo instead
X-XSS-ProtectionBrowser XSS auditors were removed; the filter itself enabled some attacksOmit it, or send 0. Use CSP.
Expect-CTDeprecated; Certificate Transparency is enforced by browsers by defaultRemove it
Public-Key-Pins (HPKP)Removed from browsers; mistakes could lock users outRemove it
Feature-PolicyRenamed and changed syntaxPermissions-Policy
P3PObsolete, ignoredRemove it

Also consider removing Server version strings and X-Powered-By. Hiding them does not fix vulnerabilities, but there is no reason to advertise your framework version.

Check the headers a URL really returns

Enter a URL to see its response headers and redirect chain, with notes on missing security headers and caching rules. Use it after a deploy to confirm the policies above took effect.

Open HTTP Headers Checker →