Transfer-Encoding vs Content-Encoding in HTTP
Understand the difference between Transfer-Encoding and Content-Encoding. Learn how chunked transfer encoding interacts with gzip compression.
Detailed Explanation
Transfer-Encoding vs Content-Encoding
These two HTTP headers are commonly confused but serve fundamentally different purposes.
Content-Encoding: About the Payload
Content-Encoding describes a transformation applied to the resource itself. The client stores the decoded version. It is an end-to-end header.
Content-Encoding: gzip
This means: "the body is the gzip-compressed form of the original resource."
Transfer-Encoding: About the Transport
Transfer-Encoding describes how the message body is formatted for transport between nodes. It is a hop-by-hop header that can be decoded by any intermediary.
Transfer-Encoding: chunked
This means: "the body is sent in chunks with size prefixes."
How They Interact
A response can use both simultaneously:
Content-Encoding: gzip
Transfer-Encoding: chunked
This means the original resource is gzip-compressed, and the compressed bytes are sent in chunks. This is common for dynamically generated, compressed responses where the server doesn’t know the total compressed size upfront.
Chunked Encoding Details
4\r\n (chunk size in hex)
Wiki\r\n (4 bytes of data)
6\r\n
pedia \r\n (6 bytes of data)
0\r\n (final chunk, size 0)
\r\n (end of message)
Key Differences Summary
| Aspect | Content-Encoding | Transfer-Encoding |
|---|---|---|
| Purpose | Resource transformation | Transport formatting |
| Scope | End-to-end | Hop-by-hop |
| Typical values | gzip, br, deflate | chunked |
| Cached? | Yes (encoded form) | No (decoded by proxy) |
| Affects Content-Length? | Yes | Replaces it |
Use Case
Debugging HTTP compression issues with tools like curl, Wireshark, or browser DevTools. Understanding the distinction helps diagnose why compression might not be working or why Content-Length is missing.