Skip to main content
Axum supports streaming response bodies using the Body type, which allows you to send data incrementally rather than buffering the entire response in memory.

Basic usage

Return a Body from any Stream<Item = Result<Bytes, E>>:

Converting bodies to streams

From request body to data stream

Example adapted from examples/stream-to-file/src/main.rs:52-128.

Body utilities

Converting body to bytes

Use to_bytes to consume a body with a size limit:

Detecting size limit errors

See 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 underlying HttpBody trait from the http-body crate:

Bytes type

The Bytes type from the bytes crate for efficient byte buffers:

BodyDataStream

Convert a Body 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