expression
Axum: consume streamed body data
Use into_data_stream to consume data buffers progressively. It discards non-data frames such as trailers. Count total bytes without buffering the complete response, and do not assume any particular transport chunking. In-memory standalone Cargo example. Expected stdout (not executed): 5 bytes Common mistake: Using a data-only stream when response trailers must be preserved.
Formal Syntax & Structure
rust syntaxrust
body.into_data_stream(); stream.next().awaitWorking 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::body::Bytes; use futures_util::{stream, StreamExt}; use std::convert::Infallible; let body = Body::from_stream(stream::iter([Ok::<_, Infallible>(Bytes::from_static(b"abc")), Ok(Bytes::from_static(b"de"))])); let mut data = body.into_data_stream(); let mut count = 0; while let Some(chunk) = data.next().await { count += chunk.unwrap().len(); } println!("{count} bytes");}