What Are HTTP Headers?
HTTP headers are key-value pairs that provide additional information about a request or response. They appear after the request/status line and before the body:
Headers control caching, authentication, content types, cookies, and much more. Understanding headers is essential for debugging HTTP issues and building robust web applications.
Header Categories
HTTP headers are traditionally grouped into four categories:
| Category | Description | Examples |
|---|---|---|
| General | Apply to both requests and responses | Date, Connection, Cache-Control |
| Request | Provide info about the request or client | Host, User-Agent, Accept, Cookie |
| Response | Provide info about the response or server | Server, Set-Cookie, WWW-Authenticate |
| Entity | Describe the body content | Content-Type, Content-Length, Content-Encoding |
General Headers
These headers apply to both requests and responses, providing general information about the message.
Date
The date and time the message was sent:
Always in GMT format. Required in responses; optional in requests.
Connection
Controls whether the network connection stays open:
keep-alive allows multiple requests over one TCP connection (HTTP/1.1 default). close signals the connection will close after this exchange.
Cache-Control
Directives for caching mechanisms:
public: Can be cached by any cacheprivate: Only browser can cache (not CDNs/proxies)no-cache: Must revalidate before using cached copyno-store: Never store the responsemax-age=N: Cache is valid for N seconds
Request Headers
These headers provide information about the request being made or the client making it.
Host
The domain name and port of the server (required in HTTP/1.1):
Essential for virtual hosting—multiple websites on the same IP address.
User-Agent
Information about the client making the request:
Used for analytics, content adaptation, and (unfortunately) browser sniffing.
Accept
Media types the client can handle:
The q value (0-1) indicates preference. Higher = more preferred.
Accept-Language
Preferred languages for the response:
Enables serving content in the user's preferred language.
Accept-Encoding
Compression algorithms the client supports:
br is Brotli compression, often more efficient than gzip.
Authorization
Credentials for authenticating the client:
Common schemes: Basic (base64 encoded), Bearer (tokens), Digest.
Cookie
Previously stored cookies sent back to the server:
Multiple cookies are separated by semicolons.
Referer
The page that linked to the requested resource:
Yes, "Referer" is a misspelling of "referrer" but it's been in the spec since the beginning.
If-None-Match / If-Modified-Since
Conditional request headers for caching:
If the resource hasn't changed, the server returns 304 Not Modified.
Response Headers
These headers provide information about the response or the server.
Server
Information about the server software:
Some administrators hide this for security reasons.
Set-Cookie
Instructs the client to store a cookie:
Path: URL path the cookie applies toHttpOnly: JavaScript cannot access the cookieSecure: Only send over HTTPSSameSite: Controls cross-site requests (Strict, Lax, None)Max-AgeorExpires: When the cookie expires
Location
URL to redirect to (used with 3xx status codes) or new resource location (201):
WWW-Authenticate
Defines the authentication method for accessing a resource (with 401):
ETag
A unique identifier for a specific version of a resource:
Used for cache validation with If-None-Match.
Allow
Lists valid HTTP methods for a resource (used with 405):
Retry-After
How long to wait before retrying (used with 429, 503):
Entity Headers
These headers describe the body of the message.
Content-Type
The media type of the body:
This is crucial—it tells the recipient how to interpret the body.
Content-Length
The size of the body in bytes:
Allows the recipient to know when the body is complete.
Content-Encoding
The compression applied to the body:
Must match what the client indicated in Accept-Encoding.
Content-Language
The natural language of the intended audience:
Content-Disposition
Suggests how to handle the content (inline or download):
attachment triggers a download; inline displays in browser.
Last-Modified
When the resource was last changed:
Used for cache validation with If-Modified-Since.
Content Negotiation
Content negotiation allows clients and servers to agree on the best representation of a resource. The client expresses preferences; the server chooses the best match.
How It Works
- Client sends Accept-* headers with preferences
- Server evaluates available representations
- Server selects the best match based on quality values
- Response includes headers indicating what was chosen
Quality Values
The q parameter (0.0 to 1.0) indicates preference:
Higher q = more preferred. Default is 1.0 if omitted.
Caching Headers
Proper caching headers dramatically improve performance and reduce server load.
Cache-Control Directives
| Directive | Meaning |
|---|---|
| public | Any cache can store the response |
| private | Only browser cache (not CDN/proxy) |
| no-cache | Must revalidate before using |
| no-store | Never cache (sensitive data) |
| max-age=N | Fresh for N seconds |
| s-maxage=N | Fresh for N seconds in shared caches |
| must-revalidate | Can't use stale after max-age |
| immutable | Content will never change |
Validation with ETag
- Server sends response with
ETag: "abc123" - Browser caches the response with the ETag
- Later: browser sends
If-None-Match: "abc123" - If unchanged: server returns 304 (use cache)
- If changed: server returns 200 with new content and ETag
Interactive Examples
The following examples demonstrate various HTTP headers in context.
Content Negotiation
Client requests JSON; server responds accordingly:
Compressed Response
Server compresses the response based on Accept-Encoding:
Cache Validation (304)
Client validates cached content using ETag:
File Download
Server triggers download with Content-Disposition:
Setting Cookies
Server sets multiple cookies with various attributes:
CORS Headers
Server allows cross-origin requests: