Module tower_http::map_request_body
source · Available on crate feature
map-request-body
only.Expand description
Apply a transformation to the request body.
§Example
use http_body_util::Full;
use bytes::Bytes;
use http::{Request, Response};
use std::convert::Infallible;
use std::{pin::Pin, task::{ready, Context, Poll}};
use tower::{ServiceBuilder, service_fn, ServiceExt, Service};
use tower_http::map_request_body::MapRequestBodyLayer;
// 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(MapRequestBodyLayer::new(BodyWrapper::new))
.service_fn(handle);
// Call the service
let request = Request::new(Full::default());
svc.ready().await?.call(request).await?;
Structs§
- Apply a transformation to the request body.
- Apply a transformation to the request body.