Request a compressed response via `Accept-Encoding` to cut transfer size dramatically — especially effective for large JSON and text responses.
Request a compressed response via `Accept-Encoding` to cut transfer size dramatically — especially effective for large JSON and text responses.
# --compressed advertises Accept-Encoding AND decompresses for you:
curl --compressed https://api.example.com/big.json
# ask explicitly (you must decode it yourself):
curl -H 'Accept-Encoding: gzip, br' https://api.example.com/big.json// Browsers send Accept-Encoding automatically and decode transparently.
// res.json()/res.text() already give you the decompressed body.
const res = await fetch('https://api.example.com/big.json');
const data = await res.json();import requests
# requests sends Accept-Encoding: gzip, deflate and decodes automatically
res = requests.get('https://api.example.com/big.json')
print(res.headers.get('Content-Encoding')) # e.g. 'gzip'
data = res.json() # already decompressedThe client advertises supported schemes with `Accept-Encoding: gzip, br`, and the server replies with the one it used in `Content-Encoding`. Text responses typically shrink 70–90%. In curl, `--compressed` both sends the header and decompresses for you; sending just `-H 'Accept-Encoding: gzip'` means you receive the compressed bytes and must decode them yourself.
Browser `fetch` and `requests` negotiate and decompress transparently, so `res.json()`/`res.text()` already give you the decoded body — no extra code needed in most libraries.
Caveats: with compression, `Content-Length` reflects the compressed size, not the original. Because the response varies by encoding, the server should add `Vary: Accept-Encoding` for cache correctness. Already-compressed formats (JPEG, PNG, mp4) and tiny responses gain little or even lose from re-compression.