rust
41 lines · 7 steps
Building a create endpoint in Axum
An Axum handler deserializes JSON, inserts a row with sqlx, and returns 201 with a Location header.
Explained by
highlit
1#[derive(Deserialize)]
2pub struct CreateArticle {
3 title: String,
4 body: String,
5}
6
7#[derive(Serialize)]
8pub struct Article {
9 id: Uuid,
10 title: String,
11 body: String,
12 created_at: DateTime<Utc>,
13}
14
15pub async fn create_article(
16 State(pool): State<PgPool>,
17 Json(payload): Json<CreateArticle>,
18) -> Result<(StatusCode, HeaderMap, Json<Article>), AppError> {
19 let article = sqlx::query_as!(
20 Article,
21 r#"
22 INSERT INTO articles (id, title, body, created_at)
23 VALUES ($1, $2, $3, now())
24 RETURNING id, title, body, created_at
25 "#,
26 Uuid::new_v4(),
27 payload.title,
28 payload.body,
29 )
30 .fetch_one(&pool)
31 .await?;
32
33 let mut headers = HeaderMap::new();
34 headers.insert(
35 LOCATION,
36 HeaderValue::from_str(&format!("/articles/{}", article.id))
37 .map_err(|_| AppError::Internal)?,
38 );
39
40 Ok((StatusCode::CREATED, headers, Json(article)))
41}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Axum extractors let a handler declare exactly what it needs from state and the request body in its signature.
- 2sqlx's query_as! maps a RETURNING row straight into a typed struct with compile-time-checked SQL.
- 3Returning a tuple of status, headers, and JSON lets one handler shape a complete HTTP response.
Related explainers
rust
use axum::{ extract::{FromRef, FromRequestParts}, http::{header, request::Parts, StatusCode}, RequestPartsExt,
How a JWT extractor works in Axum
jwt
authentication
extractors
Intermediate
8 steps
rust
use std::collections::HashMap; #[derive(Clone, Copy, PartialEq)] enum Color {
Detecting cycles with three-color DFS in Rust
graph-algorithms
cycle-detection
depth-first-search
Intermediate
9 steps
go
func UploadDocument(c *gin.Context) { fileHeader, err := c.FormFile("file") if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "file is required"})
Handling multipart uploads in Gin
multipart-upload
validation
error-handling
Intermediate
9 steps
rust
use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use axum::body::Body;
A maintenance-mode gate in Axum
middleware
shared-state
atomics
Intermediate
7 steps
rust
use axum::body::Bytes; use axum::http::{header, HeaderValue, StatusCode}; use axum::response::{IntoResponse, Response}; use serde::Serialize;
Custom Axum responses with per-user ETags
etag
trait-implementation
generics
Intermediate
7 steps
rust
use axum::{extract::State, http::StatusCode, response::IntoResponse, Json}; use serde::{Deserialize, Serialize}; use std::sync::Arc;
Batch inserts with per-item status in Axum
batch-processing
error-handling
serde
Intermediate
8 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/building-a-create-endpoint-in-axum-explained-rust-6215/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.