# HTTP Headers

> Key-value pairs sent in HTTP messages that provide critical metadata about the request or response.

Subject: Internet | Lesson 12 of 35 | 3 min read
Interactive lesson: https://codewithanimation.in/internet/http-headers
Previous: [HTTP Status Codes](https://codewithanimation.in/internet/http-status-codes.md) | Next: [HTTPS & TLS](https://codewithanimation.in/internet/https-and-tls.md)

## What it is

**HTTP Headers** are key-value pairs (e.g., `Content-Type: application/json`) sent in HTTP requests and responses. They provide essential metadata about the transaction, such as who is making the request, what format the data is in, how long it should be cached, and what security policies apply.

## Why it matters

Headers control almost every advanced feature of the web. They are responsible for Authentication (`Authorization`), caching strategies (`Cache-Control`), payload parsing (`Content-Type`), and Cross-Origin Resource Sharing (CORS).

## How it works

Headers follow a strict `<Header-Name>: <Value>` format in plain text. The keys are case-insensitive. A single empty line (CRLF) signals the end of the headers and the beginning of the body.

## Common mistake: Common mistake

CORS (Cross-Origin Resource Sharing). Beginners often think CORS is a server bug, but it is actually a security mechanism enforced by the *browser*. If the server doesn't return the `Access-Control-Allow-Origin` header in the response, the browser blocks the frontend from reading the data.

## Tip: Pro tip

Mentally group headers into three buckets: **Request Headers** (Who I am / What I want), **Response Headers** (Server info / Security rules), and **Representation Headers** (Content-Type / Content-Length of the payload).

## Try it: Try it

Toggle the switches in the Header Inspector below to see how specific headers alter the behavior of a network request.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/http-headers

## Check yourself

### Question 1

If a frontend application sends a JSON payload to an API, which header MUST it include so the server knows how to parse it?

- A. Content-Type: application/json
- B. Accept: application/json
- C. Format: json
- D. Content-Encoding: gzip

Correct answer: A

The `Content-Type` header (a representation header) tells the receiving end exactly what MIME type the payload body is formatted as.

- Why not B: The Accept header tells the server what format the client *wants back*, not what format the client is *sending*.
- Why not C: This is not a standard HTTP header.
- Why not D: Content-Encoding describes compression, not the semantic data format.

### Question 2

Which statement about CORS (Cross-Origin Resource Sharing) is true?

- A. It is a browser security feature controlled by HTTP headers sent by the server.
- B. It is a server-side firewall that blocks bad IP addresses.
- C. It is a JavaScript bug caused by using `fetch()` incorrectly.
- D. It encrypts the payload so hackers cannot intercept it.

Correct answer: A

CORS is enforced entirely by the browser. When a frontend requests data from a different domain, the browser checks if the server returned the `Access-Control-Allow-Origin` header. If not, the browser blocks the frontend code from seeing the response.

- Why not B: CORS is enforced by the client (browser), not the server.
- Why not C: CORS is an intentional security protocol, not a bug.
- Why not D: Encryption is handled by TLS/HTTPS, not CORS.

### Question 3

Which header is traditionally used to pass a JWT (JSON Web Token) to an API for authentication?

- A. Authorization: Bearer <token>
- B. Security-Token: <token>
- C. Authentication: Basic <token>
- D. Cookie: jwt=<token>

Correct answer: A

The `Authorization` header is the standard way to pass credentials. The `Bearer` scheme is specifically used for passing opaque tokens like JWTs.

- Why not B: This is not a standard HTTP header.
- Why not C: Basic auth uses base64-encoded username:password, not typically JWTs.
- Why not D: While JWTs *can* be stored in cookies, the standard header specifically designed for passing tokens programmatically is Authorization.

## Remember this

- Content-Type tells the receiver how to parse the body (e.g., JSON vs URL-encoded).
- Authorization is used to send tokens (e.g., Bearer tokens) to prove identity.
- CORS errors happen in the browser because the server didn't send the right headers.
- Header names are case-insensitive.
