← Glossary

What Is an HTTP Header?

An HTTP header is a named field of metadata that a client or server attaches to a request or response, separate from the message body. Headers describe who is asking, what is being sent, how it should be handled and how long it may be cached, and browsers tightly control which ones scripts may set.

Updated:

An HTTP header is a metadata field attached to an HTTP message that describes the message or shapes how it should be handled, without being part of the content the message carries. Request headers describe the target, accepted formats and other context. Response headers can describe content, control caching or set browser policy. The body carries content separately.

In the current HTTP specification the precise term is a field, and headers are the fields that appear in a message's header section, as opposed to the less common trailer fields that can follow the body. The familiar term “header” remains common in tools and documentation. Header names are case-insensitive in their semantics. HTTP/1.x represents them as text lines with a colon between name and value; newer versions use a different encoding. Implementations impose practical size limits.

Headers in Requests and Responses

In an HTTP/1.1 exchange, a request line identifies the method and target, followed by headers and an optional body. A response has a status line and its own fields. HTTP/2 and HTTP/3 encode equivalent information using frames and pseudo-headers.

An HTTP request header carries information about the client and what it wants. The Host field names the server, which lets one machine serve many sites from one address. User-Agent describes the client software, though its value may be reduced or deliberately changed and is not proof of identity. Accept and its relatives tell the server which content types, languages and encodings the client can handle, so the server can pick a suitable representation. Cookie carries stored values eligible for that request, whether originally set by a server or permitted script. Authorization carries credentials when a resource requires them.

An HTTP response header carries information about the answer. Content-Type states the media type of the body so the browser knows whether it is HTML, an image or JSON. Content-Length gives its size in bytes. Cache-Control instructs caches how long the response may be reused. Set-Cookie asks the browser to store a cookie. Location names a destination when the status indicates a redirect. These fields guide handling of the response.

Common Header Categories

HTTP metadata serves several distinct purposes.

CategoryPurposeExamples
Request contextDescribe the client and the targetHost, User-Agent, Referer, Authorization
Content negotiationState what the client can acceptAccept, Accept-Language, Accept-Encoding
Representation metadataDescribe the body's formatContent-Type, Content-Encoding, Content-Language
Payload and transferDescribe size and transfer framingContent-Length, Transfer-Encoding
CachingControl storage and reuseCache-Control, ETag, Expires
State managementCarry cookies in each directionCookie, Set-Cookie
Security policyInstruct the browser to enforce rulesContent-Security-Policy, Strict-Transport-Security

The same name sometimes behaves differently in a request than in a response, and some fields are defined only for one direction. Custom headers exist too; the once-common convention of prefixing experimental names with X- was formally deprecated, because such fields tend to become standard and the prefix then looks wrong, so new application-specific fields are given ordinary names and, ideally, registered.

Headers vs Bodies and Transport Metadata

Headers and the body answer different questions. The body is the resource itself: the HTML document, the image bytes, the uploaded file. The headers are statements about that resource and about the exchange. A response can have headers and no body, as with a 204 response, and a request can have headers and no body, as with most page loads. Keeping the two separate is what lets a browser decide how to treat a response, by reading its headers, before or even without consuming its body.

A further distinction concerns how far a header travels. Many fields are intended for the final recipient rather than one connection, although intermediaries can modify fields where protocol rules allow it. A few are hop-by-hop, meaningful only for a single connection between two adjacent parties, and a correct proxy removes them before passing the message on. In HTTP/1.1, Connection identifies fields that apply only to the current hop. HTTP/2 and HTTP/3 prohibit connection-specific fields instead. Compliant HTTP proxies use Via to record message forwarding, which aids tracing and loop detection, and a proxy may replace a sensitive internal hostname there with a pseudonym. This is why the set of headers a server receives is not always identical to the set the browser sent: intermediaries legitimately strip, add and, in limited cases, modify fields.

What JavaScript Can and Cannot Set

A common misconception is that a web page can send any header it likes. It cannot. When script makes a request through the fetch or XMLHttpRequest APIs, the browser reserves a set of forbidden request headers that it controls itself and silently ignores any attempt to set them. These include Host, Origin, Referer, Cookie, Content-Length, any name beginning with Sec- or Proxy-, and several others. These restrictions preserve browser control over origin and credential handling. Some related behaviour has a separate API: fetch can request a referrer through its referrer option, subject to restrictions, even though directly setting Referer is forbidden.

Script can request many other headers, such as Content-Type or custom application fields, within the API’s restrictions, but doing so has consequences under cross-origin rules. Adding a non-safelisted header to a cross-origin request triggers a preflight check in which the browser first asks the target server whether that header is permitted. So even the headers a page may set are governed by browser policy, and the server retains the final say over what it will accept. Response-header visibility also depends on context. Cross-origin responses expose only a safelist unless the server permits additional fields. Set-Cookie is not exposed through fetch or XHR, but that does not make every cookie secret: only cookies protected by HttpOnly are excluded from ordinary cookie-reading APIs.

Versions, Encoding and Practical Notes

How headers are written on the wire has changed across HTTP versions, even though their meaning is stable. In HTTP/1.x a header is literally a name, a colon and a value in text, one per line. In HTTP/2 and HTTP/3 the fields are compressed rather than sent as plain text, and the protocols require field names to be lowercase, which is why developer tools display them that way. These versions also use pseudo-header fields, names beginning with a colon such as the ones carrying the method, scheme, authority, path and status, to convey what the request and status lines carried in HTTP/1.x. Connection-specific fields like Connection and Transfer-Encoding are prohibited in HTTP/2 because the protocol frames connections differently.

Two practical points follow. First, headers are not inherently secret: over plain HTTP they travel in the clear, while HTTPS protects the headers and body on each TLS connection, so sensitive values need protected transport. TLS-terminating reverse proxies can read those values, and any onward connection requires its own protection. Second, there is no fixed universal limit on header size in the specification, but servers impose their own and may reject a request whose header section is too large, which is why pushing large amounts of data into headers rather than the body is a poor idea. When diagnosing a request, inspect the headers at the relevant hop: a browser capture and an origin-server log may describe different stages of the same exchange.

Frequently asked questions

Are headers part of the response body?

No. The header section carries metadata separately from the content. Some responses, including status 204, have no body. Redirect responses may have bodies, so a redirect is not itself a guarantee of an empty response. Trailer fields are a separate, later field section.

Can JavaScript set every request header?

No. Browsers reserve a list of forbidden request headers, including Host, Origin, Referer, Cookie and Content-Length, and ignore attempts to set them. This stops a page from forging its origin or attaching cookies the browser would not otherwise send.

Are headers protected by HTTPS?

HTTPS protects headers and content on the TLS connection. The endpoints can read them, including a reverse proxy that terminates TLS; a subsequent hop needs separate protection. Encryption does not prevent accidental disclosure through application logs or exposure to an authorised endpoint.

Why do servers sometimes see different headers than the browser sent?

Because intermediaries legitimately modify them. Proxies remove hop-by-hop fields named by the Connection header, add a Via header recording that they handled the message, and in limited cases adjust fields, so the received set may differ from the sent set.

Why are HTTP/2 header names lowercase?

HTTP/2 and HTTP/3 compress fields rather than sending plain text and require field names to be lowercase, so tools display them that way. The meaning is unchanged from HTTP/1.x, where names are case-insensitive, only the wire format differs.