Skip to main content

Body

Accessing raw request body bytes

Actix 4.9+ 5 min read

# Body (Bytes)

Sometimes you need to handle raw data that doesn't fit neatly into JSON or Forms—like webhooks, custom binary protocols, or raw text. For this, you use the web::Bytes extractor.

#Extracting Bytes

Using web::Bytes loads the entire request body into memory.

code_blocksrust
use actix_web::{post, web, HttpResponse};

#[post("/webhook")]
async fn webhook(body: web::Bytes) -> HttpResponse {
    println!("Received {} bytes", body.len());
    HttpResponse::Ok().finish()
}

#Streaming the Body

If you expect very large payloads, loading everything into memory (Bytes) can cause OOM errors. Instead, you can extract the web::Payload and process it as an asynchronous stream of byte chunks.

Was this page helpful?

Reference Implementation

The following code snippet demonstrates the authoritative pattern for this documentation.

src/main.rs
1use actix_web::{post, web, App, HttpServer, Responder, HttpResponse};
2use std::str;
3
4#[post("/echo-raw")]
5async fn echo(body: web::Bytes) -> impl Responder {
6 let text = str::from_utf8(&body).unwrap_or("Invalid UTF-8");
7 HttpResponse::Ok().body(text.to_string())
8}
9
10#[actix_web::main]
11async fn main() -> std::io::Result<()> {
12 HttpServer::new(|| App::new().service(echo))
13 .bind(("127.0.0.1", 8080))?
14 .run()
15 .await
16}
Was this page helpful?