rust 53 lines · 7 steps

Handling raw byte uploads in Axum

An Axum route accepts a raw request body, enforces a size cap, persists it, and returns a typed JSON receipt.

Explained by highlit
1use axum::{
2 body::Bytes,
3 extract::State,
4 http::StatusCode,
5 response::IntoResponse,
6 routing::post,
7 Json, Router,
8};
9use serde::Serialize;
10use tower_http::limit::RequestBodyLimitLayer;
11 
12#[derive(Clone)]
13struct AppState {
14 store: std::sync::Arc<UploadStore>,
15}
16 
17#[derive(Serialize)]
18struct UploadReceipt {
19 id: String,
20 size: usize,
21}
22 
23const MAX_UPLOAD_BYTES: usize = 2 * 1024 * 1024;
24 
25async fn upload_document(
26 State(state): State<AppState>,
27 body: Bytes,
28) -> Result<(StatusCode, Json<UploadReceipt>), StatusCode> {
29 if body.is_empty() {
30 return Err(StatusCode::BAD_REQUEST);
31 }
32 
33 let id = state
34 .store
35 .persist(&body)
36 .await
37 .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
38 
39 Ok((
40 StatusCode::CREATED,
41 Json(UploadReceipt {
42 id,
43 size: body.len(),
44 }),
45 ))
46}
47 
48pub fn documents_router(state: AppState) -> Router {
49 Router::new()
50 .route("/documents", post(upload_document))
51 .layer(RequestBodyLimitLayer::new(MAX_UPLOAD_BYTES))
52 .with_state(state)
53}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Axum extractors like State and Bytes turn request parsing into typed function arguments.
  2. 2Returning Result<T, StatusCode> lets a handler map failures to HTTP status codes ergonomically.
  3. 3Body-size limits belong in a middleware layer, not scattered through handler logic.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Handling raw byte uploads in Axum — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code