Body type, which allows you to send data incrementally rather than buffering the entire response in memory.
Basic usage
Return aBody from any Stream<Item = Result<Bytes, E>>:
Converting bodies to streams
From request body to data stream
examples/stream-to-file/src/main.rs:52-128.
Body utilities
Converting body to bytes
Useto_bytes to consume a body with a size limit:
Detecting size limit errors
axum/src/body/mod.rs:14-54.
Creating streaming responses
From static data
From a stream
Common patterns
Streaming file downloads
Proxying requests
Handling multipart streams
Body types
HttpBody trait
The underlyingHttpBody trait from the http-body crate:
Bytes type
TheBytes type from the bytes crate for efficient byte buffers:
BodyDataStream
Convert aBody to a Stream<Item = Result<Bytes, Error>>:
Performance considerations
Buffering
- Streaming avoids loading entire responses into memory
- Useful for large files, proxying, or real-time data
- Consider backpressure when streaming from slow sources
Size limits
See also
- Server-Sent Events (SSE) for event streaming
- Request body extraction for consuming request bodies
- Bytes extractor for reading raw request bodies