On this page

08. What is the use of the headers in the HTTP request?

📄 Noteseasy#http#headers#networking#basics

Short answer

Headers are key-value metadata sent alongside an HTTP request or response. They tell the other side how to interpret the message without being part of the body: who you are (Authorization, Cookie), what you are sending (Content-Type), what you accept back (Accept, Accept-Encoding), caching rules (Cache-Control, If-None-Match), and security policies (Origin, CORS and CSP headers). The body is the parcel; headers are the shipping label.

Explanation

When you post a package, the label says who it is from, who it is for, whether it is fragile, and how to handle it. The courier never opens the box; it reads the label. Servers, proxies, CDNs and browsers all read headers in the same way.

Common request headers

HeaderPurposeExample
Content-TypeFormat of the request bodyapplication/json
AcceptFormats the client can handleapplication/json
AuthorizationCredentialsBearer eyJ...
CookieCookies the browser attachessession=abc
Accept-EncodingCompression supportedgzip, br
Accept-LanguagePreferred languageen-IN,en;q=0.9
If-None-Match / If-Modified-SinceConditional request for caching"etag-123"
Origin / RefererWhere the request came fromhttps://app.example.com
User-AgentClient softwarebrowser string
Custom (X-Request-Id)App-specific metadatatracing IDs

Common response headers

HeaderPurpose
Content-TypeFormat of the response
Cache-Control, ETagCaching behavior
Set-CookieStore a cookie (with HttpOnly, Secure, SameSite)
Access-Control-Allow-OriginCORS permission
Content-Security-Policy, Strict-Transport-SecuritySecurity policies
Retry-AfterWhen to retry after 429/503
LocationRedirect target

Things worth knowing

  • Header names are case-insensitive. HTTP/2 and HTTP/3 send them lowercase and compress them (HPACK/QPACK).
  • Some headers are forbidden for JS to set (Cookie, Host, Origin, Content-Length); the browser controls them.
  • Adding custom headers or Content-Type: application/json on cross-origin requests triggers a CORS preflight (OPTIONS).
  • To read custom response headers cross-origin, the server must list them in Access-Control-Expose-Headers.

Example

async function createTodo(title, token) {
  const res = await fetch("https://api.example.com/todos", {
    method: "POST",
    headers: {
      "Content-Type": "application/json", // body is JSON
      Accept: "application/json", // we want JSON back
      Authorization: `Bearer ${token}`, // who we are
      "Idempotency-Key": crypto.randomUUID(), // safe retries
    },
    body: JSON.stringify({ title }),
  });

  console.log(res.headers.get("content-type")); // read a response header
  if (!res.ok) throw new Error(`Failed: ${res.status}`);
  return res.json();
}

Common mistakes

  • Forgetting Content-Type: application/json, so the server cannot parse the body.
  • Setting Content-Type manually when sending FormData; the browser must set it to include the multipart boundary.
  • Putting secrets in custom headers to third-party domains, or tokens in URLs instead of headers.
  • Not understanding why a custom header suddenly causes a CORS preflight failure.

Follow-up questions

  • What is a CORS preflight? An automatic OPTIONS request the browser sends before a "non-simple" cross-origin request to check that the server allows the method and headers.
  • How do ETag and If-None-Match work? The server sends an ETag; the browser sends it back on the next request, and the server replies 304 Not Modified with no body if nothing changed.
  • Why can't fetch set the Cookie header? It is a forbidden header; use credentials: "include" and let the browser attach cookies.

Quick recap

  • Headers are metadata: the label, not the parcel.
  • They handle content negotiation, auth, caching, compression and security.
  • Some headers are browser-controlled.
  • Custom headers on cross-origin calls trigger CORS preflights.