pub struct Payload(pub BodyStream);Expand description
The request body as a stream, without buffering it.
Consumes the body, so it must be the last handler argument. Use this for a
large upload that should not be held in memory — the counterpart to
Json<T> and Form<T>, which buffer.
use churust_core::{Churust, Payload, TestClient};
use futures_util::StreamExt;
let app = Churust::server()
.routing(|r| {
r.post("/count", |Payload(mut body): Payload| async move {
let mut n = 0usize;
while let Some(Ok(chunk)) = body.next().await {
n += chunk.len();
}
format!("{n} bytes")
});
})
.build();
let res = TestClient::new(app).post("/count").body("hello").send().await;
assert_eq!(res.text(), "5 bytes");Both the server-wide max_body_bytes and any per-route
max_body_bytes apply. Because the
response may already have begun, exceeding either arrives as an error item
in the stream rather than as a 413 — propagate it with ? to turn it back
into the right status.
The stream is bounded; collecting it into memory is your allocation, not the framework’s. A handler that gathers the whole body allocates up to whichever limit is tighter, so tighten the route where a large ceiling was raised for some other route’s benefit.
Tuple Fields§
§0: BodyStreamThe body’s chunks.