
What Is the Content-Length Header?
The Content-Length header describes the size of an HTTP message’s content, expressed in bytes. It communicates that value in requests and responses, helping recipients understand how much data to expect.
For example, Content-Length: 245760 declares a content length of 245,760 bytes. The value is a non-negative decimal integer, without a unit suffix. To send this header accurately, the sender must know the relevant size before transmitting the headers.
In media delivery, Content-Length can describe an image, a video segment, or another downloadable resource. It measures data size rather than image dimensions, video duration, or visual quality.
How the Content-Length Header Works
Content-Length measures encoded bytes. A text string’s character count may differ from its byte count because some character encodings represent individual characters using multiple bytes.
The value excludes the HTTP headers and network overhead. For a multipart upload, however, the body includes boundaries and part headers alongside the uploaded files. Its content length therefore exceeds the combined sizes of the files alone.
When content encoding such as gzip or Brotli compresses a response, Content-Length describes the encoded content being delivered, not its decompressed size. Representation metadata applies to that encoded form unless specified otherwise.
For example, an SVG might occupy one size before HTTP compression and a smaller size during delivery. Measuring the decompressed document would produce the wrong value for the compressed response.
Any processing that changes the outgoing body requires the length to be recalculated or omitted where the protocol permits.
When Can Content-Length Be Omitted?
HTTP can determine message boundaries through mechanisms other than Content-Length. It’s required by HTTP/1.0, but has changed with newer versions.
HTTP/1.1 and Chunked Transfer Encoding
When a response’s final size is unknown, HTTP/1.1 can use chunked transfer encoding instead via Transfer-Encoding: chunked. This sends content in separately sized chunks and signals completion without declaring the total beforehand.
A sender must not include Content-Length alongside Transfer-Encoding. For an ordinary body-bearing message framed by Content-Length, receiving fewer bytes than declared means the message is incomplete.
HTTP/2 and HTTP/3
HTTP/2 and HTTP/3 use protocol framing to carry content and signal message completion, so they don’t need HTTP/1.1 chunked transfer encoding. Content-Length can still be supplied, but it’s made redundant by DATA frames. However, it can still be included for backwards compatibility.
Why Content-Length Matters
For media optimization, it provides a useful measurement of the delivered representation. A thumbnail and a large display image derived from the same source can have very different lengths.
That comparison needs context. A smaller response may reflect different dimensions, formatting, or compression settings. Content-Length alone cannot establish that one version provides an acceptable viewing experience.
