Module tower_http::map_response_body
source · Expand description
Apply a transformation to the response body.
§Example
use bytes::Bytes;
use http::{Request, Response};
use http_body_util::Full;
use std::convert::Infallible;
use std::{pin::Pin, task::{ready, Context, Poll}};
use tower::{ServiceBuilder, service_fn, ServiceExt, Service};
use tower_http::map_response_body::MapResponseBodyLayer;
// A wrapper for a `Full<Bytes>`
struct BodyWrapper {
inner: Full<Bytes>,
}
impl BodyWrapper {
fn new(inner: Full<Bytes>) -> Self {
Self { inner }
}
}
impl http_body::Body for BodyWrapper {
// ...
}
async fn handle<B>(_: Request<B>) -> Result<Response<Full<Bytes>>, Infallible> {
// ...
}
let mut svc = ServiceBuilder::new()
// Wrap response bodies in `BodyWrapper`
.layer(MapResponseBodyLayer::new(BodyWrapper::new))
.service_fn(handle);
// Call the service
let request = Request::new(Full::<Bytes>::from("foobar"));
svc.ready().await?.call(request).await?;
Structs§
- Apply a transformation to the response body.
- Apply a transformation to the response body.
- Response future for
MapResponseBody
.