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
| Feature | Codec | Crate |
|---|---|---|
gzip | gzip | flate2 |
deflate | deflate | flate2 |
brotli | br | brotli |
zstd | zstd | zstd |
[dependencies]
aioduct = { version = "0.2.5", features = ["tokio", "rustls", "rustls-ring", "gzip", "brotli"] }
How It Works
When any decompression feature is enabled:
- The client adds an
Accept-Encodingheader to outgoing requests listing the enabled codecs (unless you already set one). - If the response has a matching
Content-Encoding, the body is transparently decompressed. - The
Content-EncodingandContent-Lengthheaders 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.