rust
58 lines · 7 steps
A strict JSON extractor in Axum
Building a custom Axum extractor that rejects requests unless they carry a real JSON Content-Type.
Explained by
highlit
1use axum::{
2 async_trait,
3 body::Bytes,
4 extract::FromRequest,
5 http::{header::CONTENT_TYPE, Request, StatusCode},
6 response::{IntoResponse, Response},
7 Json,
8};
9use serde::de::DeserializeOwned;
10
11pub struct StrictJson<T>(pub T);
12
13#[async_trait]
14impl<T, S, B> FromRequest<S, B> for StrictJson<T>
15where
16 T: DeserializeOwned,
17 B: axum::body::HttpBody + Send + 'static,
18 B::Data: Send,
19 B::Error: std::error::Error + Send + Sync + 'static,
20 S: Send + Sync,
21{
22 type Rejection = Response;
23
24 async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
25 let content_type = req
26 .headers()
27 .get(CONTENT_TYPE)
28 .and_then(|value| value.to_str().ok())
29 .ok_or_else(|| {
30 (StatusCode::BAD_REQUEST, "missing Content-Type header").into_response()
31 })?;
32
33 let mime: mime::Mime = content_type.parse().map_err(|_| {
34 (StatusCode::BAD_REQUEST, "malformed Content-Type header").into_response()
35 })?;
36
37 let is_json = mime.type_() == mime::APPLICATION
38 && (mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON));
39
40 if !is_json {
41 return Err((
42 StatusCode::UNSUPPORTED_MEDIA_TYPE,
43 "expected Content-Type: application/json",
44 )
45 .into_response());
46 }
47
48 let bytes = Bytes::from_request(req, state)
49 .await
50 .map_err(IntoResponse::into_response)?;
51
52 let value = serde_json::from_slice(&bytes).map_err(|err| {
53 (StatusCode::UNPROCESSABLE_ENTITY, format!("invalid JSON body: {err}")).into_response()
54 })?;
55
56 Ok(StrictJson(value))
57 }
58}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Implementing FromRequest lets you plug custom validation directly into a handler's argument list.
- 2Parsing the Content-Type as a real MIME type is safer than a naive string equality check.
- 3Returning Response as the Rejection type gives you full control over each failure's status code and message.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
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/a-strict-json-extractor-in-axum-explained-rust-769a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.