Why Is a Proxy Response Garbled? Fixing gzip and Brotli

Published:

10 minute read

Acar Diveroli
Written by: Acar Diveroli
Tall response window with a content-encoding: br header and undecoded bytes; a blue br decoder card sits in front

Content-Encoding is the HTTP header in which the server states how it compressed the response body. The client lists the methods it supports in the Accept-Encoding header, the server picks one and the client decompresses the body. Over HTTPS the proxy cannot see this exchange: only encrypted bytes pass through the tunnel. If garbled characters show up on screen, the client cannot decode the method the server chose. This post covers how the headers work, the traps in requests, HTTPX, curl and Node, and how compression affects traffic.

Compression negotiation and the proxy

Compression starts with a negotiation. The client puts a list such as Accept-Encoding: gzip, deflate, br in the request. The server picks one method from the list, compresses the body with it and reports its choice in the Content-Encoding header. The client reads that header and decompresses the body.

The trap is in the list itself. If the client lists a method it can't decode, the server may pick exactly that one. Sometimes the server also answers with a method that isn't on the list at all. In both cases the body stays compressed.

ValueMethodFirst bytes of the body
gzipgzip1f 8b
deflatedeflate in a zlib wrapperUsually 78
brBrotli (RFC 7932)No fixed signature
zstdZstandard (RFC 8878)28 b5 2f fd

These signatures help with diagnosis. If the body starts with 1f 8b, what you have is undecoded gzip.

On an HTTPS request the proxy can't see the compression. The client asks the proxy for a CONNECT tunnel and sets up TLS inside it. Both headers stay inside the encrypted traffic. The proxy only carries encrypted bytes in both directions.

Over plain HTTP the proxy does see the headers. An ordinary forward proxy still passes them on unchanged. Through our test proxy, requests' gzip, deflate, br list reached the target unchanged, and the Brotli response came back to the client byte for byte. If you see a garbled body, look for the fault on the client side. Proxy options for HTTPS traffic are on the HTTPS Proxy page.

Python: requests and HTTPX

Brotli support in Python depends on a separate package. requests leaves decompression to urllib3. If urllib3 finds the brotli or brotlicffi package, it both lists br and decodes it. Without the package, br drops off the list and the default request works fine.

The problem appears when the server answers with br even though it isn't on the list. In our test, in an environment without the brotli package, requests raised no error. r.content returned 1,563 bytes of compressed data as is, and r.json() stopped with a JSONDecodeError. HTTPX 0.28.1 raised a UnicodeDecodeError in the same situation. Neither message mentions compression. That is why the problem is often mistaken for a character encoding error.

zstd support depends on the library and the Python version:

LibraryFor brFor zstd (Python 3.13)
requests 2.34 / urllib3 2.6brotli or brotlicffibackports.zstd
HTTPX 0.28brotli or brotlicffizstandard
Node.js 24 fetchBuilt inBuilt in

In our test we switched the packages on and off one at a time. zstandard affected only HTTPX, backports.zstd only requests. According to the urllib3 changelog, Python 3.14 uses the compression.zstd module from the standard library. Install the missing packages through extras:

bash
pip install "urllib3[brotli,zstd]"
pip install "httpx[brotli,zstd]"

Then check what the request sent and what the server chose:

python
import requests

proxy = "http://user:pass@pr.proxynet.io:8000"
proxies = {"http": proxy, "https": proxy}

r = requests.get("https://example.com/", proxies=proxies, timeout=30)
print(r.request.headers["Accept-Encoding"])  # list that was sent
print(r.headers.get("Content-Encoding"))      # what the server chose
print(r.content[:12])                         # start of the decoded body

The second trap comes with stream=True. r.raw.read() reads the raw bytes. In our test, a gzip response returned 3,473 bytes and the 1f 8b signature. r.raw.read(decode_content=True) returned the decoded body. How to pass a proxy to requests is covered in using a proxy with Python Requests.

A hand-written Accept-Encoding

Writing the header by hand overrides the client's automatic list. The Accept-Encoding: gzip, deflate, br, zstd line copied from a browser is the most common example. The server picks the best method from the list and may send one the client can't decode.

In our test we gave requests this line. Our server chose zstd. In an environment without a zstd package, the body stayed as 2,243 bytes starting with 28 b5 2f fd. In curl, writing -H "Accept-Encoding: br" together with --compressed made the server send Brotli. curl stopped with the error (61) Unrecognized content encoding type.

Node.js fetch was not affected by this trap. It also decoded a zstd response sent for a hand-written gzip, br, zstd list. Still, the rule is simple: leave the Accept-Encoding header to the library. If your code has a header list copied from a browser, delete this line first.

curl and --compressed

By default curl sends no Accept-Encoding and does not decompress the body. The --compressed flag does both jobs: it asks for the supported methods and decodes the response. If you write -H "Accept-Encoding: gzip" without the flag, the file gets gzip data starting with 1f 8b.

bash
curl --compressed \
  -x http://user:pass@pr.proxynet.io:8000 \
  https://example.com/ -o page.html

The methods requested depend on how curl was built. For curl 8.22.0 on Windows 11, the Features line of curl -V listed only libz. This build asked for deflate, gzip with --compressed. When we forced the server to send br, it returned error (61). If you see brotli and zstd on the Features line, your curl also asks for and decodes those methods. All of curl's proxy options are in using a proxy with cURL.

Node.js fetch

The built-in fetch in Node.js decompresses the body itself. In our test, Node 24.22.2 (undici 7.30.1) decoded all four response types, gzip, deflate, br and zstd, without any extra package. r.json() returned the correct data every time.

The list it sent depended on the URL. An HTTPS URL got br, gzip, deflate, a plain HTTP URL got gzip, deflate. It did not list zstd even though it can decode it. In Node 24, NODE_USE_ENV_PROXY=1 makes fetch read the HTTPS_PROXY variable:

bash
NODE_USE_ENV_PROXY=1 \
HTTPS_PROXY=http://user:pass@pr.proxynet.io:8000 \
node app.mjs
js
// app.mjs
const r = await fetch('https://example.com/api');
console.log(r.headers.get('content-encoding')); // what the server chose
const data = await r.json();                    // body already decoded

The low-level node:https module, on the other hand, decodes nothing. When you set the accept-encoding header by hand, the body arrives as gzip starting with 1f 8b. In that case you decode it yourself with zlib.gunzipSync or zlib.brotliDecompressSync.

Compression and traffic

The proxy carries the compressed bytes, and decompression happens on your machine. That makes compression a direct saving on traffic billed per GB. At Proxynet, Residential Proxy and mobile proxies are billed per GB.

Our test proxy counted the bytes passing through the tunnel. A 29,136-byte JSON response took 31,376 bytes uncompressed. The same response dropped to 5,714 bytes with gzip and 4,868 with Brotli. Part of the difference is the TLS handshake, which you pay on every new connection.

The ratio depends on the content. Repetitive JSON and HTML compress well. Images and video are already compressed formats, so you see no gain there. Our measurements with real pages are in how to calculate proxy bandwidth.

Troubleshooting

With a garbled body, check in this order:

  1. Print the response's Content-Encoding header. If there is no value, the problem isn't compression; check the character encoding.
  2. Look at the first four bytes of the body. 1f 8b means gzip, 28 b5 2f fd means zstd.
  3. Print the Accept-Encoding value the request sent. In requests use r.request.headers, in curl the -v output.
  4. If your code has a hand-written Accept-Encoding line, delete it.
  5. In Python, check the package: if python -c "import brotli" fails, the package isn't installed.
  6. In curl, check the Features line of curl -V.

The symptom may be a JSONDecodeError, a UnicodeDecodeError or � marks on screen. In all three cases the first step is the same: look at the header and the first bytes.

How we tested

On December 1, 2026, we set up a local test rig written in Node.js on Windows 11. The target server sent the same 29,136-byte JSON body with gzip, deflate, br or zstd. It picked the method from the client's list or was forced by a URL parameter. In between sat a proxy that forwarded CONNECT tunnels to an HTTPS target with a self-signed certificate and counted the bytes passing through.

On the Python side we used Python 3.13.17, requests 2.34.2 (urllib3 2.6.3), HTTPX 0.28.1, brotli 1.2.0, backports.zstd 1.8.1 and zstandard 0.25.0. The other clients were curl 8.22.0 (Schannel, libz only) and Node.js 24.22.2 (undici 7.30.1). We simulated a missing package by blocking the module import.

The bottom line

Leave compression to the library, install the brotli package in Python and use --compressed in curl. Above all, don't write the Accept-Encoding header by hand. It is the shortest way to ask for a method the client can't decode.

Sources

Frequently asked questions

Does a proxy compress or corrupt the response?

No. On an HTTPS request the proxy only carries the encrypted CONNECT tunnel and can't see the body. Over plain HTTP, an ordinary forward proxy passes the headers and body on unchanged. In our test, gzip and Brotli responses reached the client byte for byte. A garbled body is usually caused by a Content-Encoding value the client can't decode.

Why doesn't requests decode a Brotli response?

Because the brotli package isn't installed. urllib3 decodes br only if the brotli or brotlicffi package is present. If the server sends Brotli anyway while the package is missing, r.content stays compressed and r.json() fails. Install the package with pip install "urllib3[brotli]". After that, the default list becomes gzip, deflate, br.

Why is curl output full of garbage characters?

Because the response arrived compressed and you didn't tell curl to decode it. If you write -H "Accept-Encoding: gzip" without --compressed, curl saves the gzip data as is. Delete the hand-written header and add --compressed. curl then asks for the methods it supports and decodes the response. If a method it doesn't support arrives, it returns error (61).

Does Node.js fetch decompress responses itself?

Yes. The built-in fetch in Node.js decodes gzip, deflate, Brotli and zstd responses automatically. In our test, Node 24.22.2 decoded all four without any extra package. It sent br, gzip, deflate to HTTPS URLs and gzip, deflate to plain HTTP URLs. The low-level node:https module doesn't decode, so there you need the zlib module.

How do I decode zstd responses in Python?

By installing the package your library expects. urllib3 2.6 needs the backports.zstd package on Python 3.13 and earlier. pip install "urllib3[zstd]" installs it. On Python 3.14 the standard library module is used. HTTPX 0.28 needs the zstandard package, installed with pip install "httpx[zstd]". In our test, each package affected only its own library.

Does compression reduce proxy traffic?

Yes, noticeably for text responses. The proxy carries the compressed bytes, and decompression happens on your machine. In our test, a 29 KB JSON response took 31.4 KB in the tunnel uncompressed, 5.7 KB with gzip and 4.9 KB with Brotli. Images and video are already compressed, so they show no such gain. On traffic paid per GB, the difference shows up directly on the bill.

Ask ChatGPTAsk Claude