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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Axum extractors like State and Bytes turn request parsing into typed function arguments.
- 2Returning Result<T, StatusCode> lets a handler map failures to HTTP status codes ergonomically.
- 3Body-size limits belong in a middleware layer, not scattered through handler logic.
Related explainers
rust
use axum::{extract::{Path, State}, http::StatusCode, Json}; use dashmap::DashMap; use serde::Serialize; use std::sync::Arc;
Request coalescing in an Axum handler
caching
concurrency
request-coalescing
Advanced
8 steps
ruby
class TemplateInterpolator PLACEHOLDER = /\{\{\s*([\w.]+)\s*\}\}/ def initialize(strict: false)
Interpolating templates with dotted keys in Ruby
regex
string-interpolation
hash-traversal
Intermediate
6 steps
go
package config import ( "fmt"
A thread-safe config singleton in Go
singleton
concurrency
environment-variables
Intermediate
7 steps
rust
use std::net::Ipv4Addr; use std::str::FromStr; #[derive(Debug, Clone, Copy)]
Parsing and matching IPv4 CIDR ranges in Rust
bitwise-operations
parsing
error-handling
Intermediate
8 steps
rust
use std::sync::OnceLock; static CRC_TABLE: OnceLock<[u32; 256]> = OnceLock::new();
Table-driven CRC32 with lazy init in Rust
checksums
lazy-initialization
lookup-tables
Intermediate
8 steps
typescript
import { parsePhoneNumberFromString, CountryCode } from 'libphonenumber-js'; export interface NormalizedPhone { e164: string;
Normalizing phone numbers to E.164 in TypeScript
validation
normalization
error-handling
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/handling-raw-byte-uploads-in-axum-explained-rust-0f15/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.