rama::http::layer

Module map_request_body

Expand description

Apply a transformation to the request body.

§Example

use rama_http::{Body, Request, Response};
use rama_http::dep::http_body;
use bytes::Bytes;
use std::convert::Infallible;
use std::{pin::Pin, task::{ready, Context, Poll}};
use rama_core::{Layer, Service, context};
use rama_core::service::service_fn;
use rama_http::layer::map_request_body::MapRequestBodyLayer;
use rama_core::error::BoxError;

// A wrapper for a `Full<Bytes>`
struct BodyWrapper {
    inner: Body,
}

impl BodyWrapper {
    fn new(inner: Body) -> Self {
        Self { inner }
    }
}

impl http_body::Body for BodyWrapper {
    // ...
}

async fn handle<B>(_: Request<B>) -> Result<Response, Infallible> {
    // ...
}

let mut svc = (
    // Wrap response bodies in `BodyWrapper`
    MapRequestBodyLayer::new(BodyWrapper::new),
).layer(service_fn(handle));

// Call the service
let request = Request::new(Body::default());

svc.serve(context::Context::default(), request).await?;

Structs§