Content-Encoding Header

The Content-Encoding header is used by the server to tell the client which encoding is applied to the message body. The content encoding is mainly used for a document compression that will not lead to data loss, save bandwidth and speed up the website.

The general recommendation is to compress data whenever possible, but some types of resources, such as JPEG images, are already compressed, and using additional compression will have no effect.

Content-Encoding Syntax and Examples
Content-Encoding: identity
Content-Encoding: gzip      
Content-Encoding: compress
Content-Encoding: deflate
Content-Encoding: br

If multiple encodings are applied to an entity, the content encodings MUST be listed in the order in which they were applied Additional information about the encoding parameters MAY be provided by other fields of the message header.

If the message doesn't contain the "no-transform" cache-control directive, and if the recipient can accept new encodings, then a non-transparent proxy may modify the content-coding of the entity.

Content-Encoding Directives

  • identity: no transformation is used, the default value for content coding.
  • gzip: GNU zip format uses the deflate algorithm for compression.
  • compress: UNIX "compress" program method, deprecated and replaced by gzip or deflate.
  • deflate: compression based on the deflate algorithm.
  • br: a modern, effective compression algorithm specifically designed for HTTP.
The client sends a list of supported compression schemes in the "Accept-Encoding" request header field, for example, "Accept-Encoding: gzip, deflate, br". The server may select any of these schemes and indicate the selected scheme in the "Content-Encoding" response header, for example, "Content-Encoding: br". If the server does not accept any advised content-coding types, it may respond with a status code of 415 (Unsupported Media Type).

GET Request With Accept-Encoding Header Live Request
GET / HTTP/1.1
Accept-Encoding: gzip

And the server response with Content-Encoding header.

Server Response With Content-Encoding Header
HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8