Course contentsShow
← Web Development
Lesson 2134 of 2,98239. Network PerformanceFree lesson

Content-Encoding and Accept-Encoding Headers

Master the HTTP headers that control compression negotiation between browsers and servers for optimal delivery.

Content-Encoding and Accept-Encoding Headers

You'll learn: How browsers and servers negotiate compression formats using HTTP headers to deliver optimally compressed content.

The Negotiation Handshake

Think of compression negotiation like ordering at a restaurant with dietary restrictions. You (the browser) tell the waiter (server) what formats you can handle, and the kitchen sends back food in one of those formats—plus a label telling you which one they chose.

The Accept-Encoding header is your browser's way of saying "I can digest these compression formats." When your browser makes an HTTP request, it includes something like:

Accept-Encoding: gzip, deflate, br

This tells the server: "I understand Gzip, Deflate, and Brotli compression—send me whichever you prefer."

The Server's Response

The server examines this header and compresses the response using one of the supported algorithms (usually choosing the most efficient one available, like Brotli). It then includes a Content-Encoding header in the response:

Content-Encoding: br

This tells your browser: "Hey, I compressed this response with Brotli—decode it accordingly."

Why This Matters

Without these headers working together, compression wouldn't work. If a server sent Brotli-compressed content but the browser didn't know to decompress it, you'd see gibberish. Similarly, if a browser requested Brotli but the server doesn't support it, the server knows to fall back to Gzip or send uncompressed content.

The browser automatically handles decompression based on the Content-Encoding value—you don't need to write JavaScript to decompress responses. This happens transparently before your code even sees the data.

Key Takeaway: Accept-Encoding lets browsers advertise supported compression formats, while Content-Encoding tells browsers which format was actually used—enabling transparent, efficient content delivery without breaking compatibility.