Skip to main content

Module body_limit

Module body_limit 

Available on crate features http and std only.
Expand description

Apply a request-body limit at the HTTP application layer.

This is distinct from crate::BodyLimitLayer, which attaches a connection-wide request/response policy to a transport before HTTP is decoded. Rama’s H1/H2 adapter enforces that transport policy. This layer is useful for a stricter per-route or per-service request limit.

When both layers are present, the smaller request limit wins. This layer detects an equal or stricter inherited transport limit and avoids wrapping the request body a second time.

§Example

use rama_http::{Body, Request, Response};
use std::convert::Infallible;
use rama_core::service::service_fn;
use rama_core::{Layer, Service};
use rama_http::layer::body_limit::BodyLimitLayer;

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

let mut svc = (
     // Limit the request body to 2MB
    BodyLimitLayer::new(2*1024*1024),
).into_layer(service_fn(handle));

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

svc.serve(request).await?;

Structs§

BodyLimitLayer
Apply a limit to the request body’s size.
BodyLimitService
Apply a transformation to the request body.