How to accept gzip with Symfony HTTP Client

How to accept gzip with Symfony HTTP Client banner

Recently, I wrote an HTTP parser using Symfony HTTP client, and I wanted to save some bandwidth because I was using proxies.

One way to save bandwidth is to fetch compressed responses and then decompress them.

HTTP servers and clients use Gzip/deflate or Brotli.

How to fetch gzipped content with Symfony HTTP client?

It turns out that, by default, Symfony HTTP adds the Accept-Encoding: gzip header, which is quite handy. Good job to the Symfony team!

However, this header is added only if:

  • cURL is compiled with zlib - check with php --ri curl
  • When a native client is used, PHP must be compiled with Zlib - check with php -i | grep zlib

HTTP Client compression

What is HTTP compression?

Compression in HTTP (Hypertext Transfer Protocol) refers to the process of reducing the size of data before it is transmitted over the network from a web server to a web client (usually a web browser) or vice versa. The primary goal of compression is to optimize the efficiency and speed of data transfer over the Internet.

HTTP compression is particularly important because it helps reduce the amount of data that needs to be transmitted, reducing the time it takes to load web pages and improving overall website performance. This is especially valuable in today's internet landscape, where web pages often contain a variety of resources such as text, images, stylesheets, scripts, and more.

Two common compression techniques used in HTTP are:

  1. Gzip Compression: Gzip (short for GNU Zip) is a widely used compression method in HTTP. When a web server receives an HTTP request from a client, it can check the Accept-Encoding header in the request to see if the client supports gzip compression. If the client supports it, the server can compress the response data (such as HTML, CSS, JavaScript, or other resources) before sending it back to the client. The client's browser then decompresses the received data and renders the web page.

  2. Brotli Compression: Brotli is a newer compression algorithm developed by Google. It offers better compression ratios than Gzip, which means it can reduce the size of files more effectively. Like Gzip, Brotli can also be used in HTTP by checking the Accept-Encoding header in the client's request. If the client supports Brotli, the server can use it to compress the response data.

Compression in HTTP offers several advantages:

  • Faster Page Loading: Smaller file sizes mean faster download times, which leads to quicker page rendering and a better user experience.

  • Reduced Bandwidth Usage: By transmitting compressed data, websites can significantly reduce their bandwidth consumption, which can be cost-effective for both the website owner and the end user.

  • Improved Mobile Experience: Compression is particularly valuable for mobile users, as it reduces the amount of data needed to load a page, saving both time and mobile data usage.

In summary, compression in HTTP is a technique used to reduce the size of data exchanged between web servers and clients, leading to faster load times, reduced bandwidth usage, and improved overall web performance. Gzip and Brotli are two common compression methods employed to achieve these benefits.