expression

Axum: extractor body size limit

Limit buffering for extractors that honor the Axum default body limit, including String and Bytes. The tiny four-byte threshold is chosen only to make the boundary easy to test. In-memory standalone Cargo example. Expected stdout (not executed): 200 413 Common mistake: Assuming the extractor-specific limit covers every raw streaming body consumer.

Formal Syntax & Structure

rust syntaxrust
DefaultBodyLimit::max(bytes)

Working Example

Example Snippetrust
use axum::{Router, body::{Body, to_bytes}, http::{Request, StatusCode}};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() {    use axum::{routing::post, extract::DefaultBodyLimit};    let app = Router::new().route("/", post(|body: String| async move { body.len().to_string() })).layer(DefaultBodyLimit::max(4));    for body in ["1234", "12345"] {        let r = app.clone().oneshot(Request::builder().method("POST").body(Body::from(body)).unwrap()).await.unwrap();        println!("{}", r.status().as_u16());    }}
Rust Reference