Skip to content

Compression

One flag makes the transport compress responses when the client asks for it. HttpConfig.compression turns on poem’s negotiation layer: each request’s Accept-Encoding header picks gzip, deflate, brotli or zstd, and the response body is encoded before it leaves the process. Off by default — in most deployments a reverse proxy already does this.

apps/api/src/module.rs (abridged)
HttpModule::for_root(HttpConfig {
port: 3002,
compression: true,
..Default::default()
}),

Like every HttpConfig field, the flag is dual-path — pin it in code as above, or set it in the environment:

Terminal window
NESTRS_HTTP__COMPRESSION=true nestrs run dev api

A client that advertises support gets an encoded body; one that does not gets plain bytes — the same route, negotiated per request:

Terminal window
$ curl -sS -H 'Accept-Encoding: gzip' -D - -o /dev/null localhost:3002/api-json
HTTP/1.1 200 OK
content-type: application/json
content-encoding: gzip
$ curl -sS -D - -o /dev/null localhost:3002/api-json
HTTP/1.1 200 OK
content-type: application/json
  • The layer sits inside CORS, so a preflight (OPTIONS, no body) is never compressed, and outside the handler, so what the handler returns is what gets encoded.
  • A response already carrying Content-Encoding is left untouched.
  • Turn it off (the default) when a proxy, CDN, or load balancer in front of the app compresses — doing it twice only burns CPU.