Available on crate features decompression-br or decompression-deflate or decompression-gzip only.
Expand description

Middleware that decompresses response bodies.

Example

use bytes::BytesMut;
use http::{Request, Response};
use http_body::Body as _; // for Body::data
use hyper::Body;
use std::convert::Infallible;
use tower::{Service, ServiceExt, ServiceBuilder, service_fn};
use tower_http::{compression::Compression, decompression::DecompressionLayer, BoxError};

// Some opaque service that applies compression.
let service = Compression::new(service_fn(handle));

// Our HTTP client.
let mut client = ServiceBuilder::new()
    // Automatically decompress response bodies.
    .layer(DecompressionLayer::new())
    .service(service);

// Call the service.
//
// `DecompressionLayer` takes care of setting `Accept-Encoding`.
let request = Request::new(Body::empty());

let response = client
    .ready()
    .await?
    .call(request)
    .await?;

// Read the body
let mut body = response.into_body();
let mut bytes = BytesMut::new();
while let Some(chunk) = body.data().await {
    let chunk = chunk?;
    bytes.extend_from_slice(&chunk[..]);
}
let body = String::from_utf8(bytes.to_vec()).map_err(Into::<BoxError>::into)?;

assert_eq!(body, "Hello, World!");

Structs

Decompresses response bodies of the underlying service.
Decompresses response bodies of the underlying service.
Response future of Decompression.