Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Response Decompression

aioduct can automatically decompress response bodies based on the Content-Encoding header. Each compression algorithm is gated behind its own feature flag.

Feature Flags

FeatureCodecCrate
gzipgzipflate2
deflatedeflateflate2
brotlibrbrotli
zstdzstdzstd
[dependencies]
aioduct = { version = "0.2.5", features = ["tokio", "rustls", "rustls-ring", "gzip", "brotli"] }

How It Works

When any decompression feature is enabled:

  1. The client adds an Accept-Encoding header to outgoing requests listing the enabled codecs (unless you already set one).
  2. If the response has a matching Content-Encoding, the body is transparently decompressed.
  3. The Content-Encoding and Content-Length headers are removed from the decompressed response.
use aioduct::TokioClient;

#[tokio::main]
async fn main() -> Result<(), aioduct::Error> {
    // With the `gzip` feature enabled, gzip responses are decompressed automatically
    let client = TokioClient::with_rustls();

    let text = client.get("https://httpbin.org/gzip")?
        .send().await?
        .text().await?;
    println!("{text}");
    Ok(())
}

Disabling Decompression

Use no_decompression() on the builder to disable all automatic decompression. The raw compressed bytes are returned as-is.

#![allow(unused)]
fn main() {
use aioduct::TokioClient;
let client = TokioClient::builder()
    .no_decompression()
    .build()?;
}

Supported Encodings

The Accept-Encoding header is built from the enabled features. For example, with gzip and brotli enabled, outgoing requests include:

Accept-Encoding: zstd, gzip, deflate, br

Only codecs whose feature flag is compiled in will appear. If you set Accept-Encoding manually on a request, the client will not overwrite it.