expression
REST: creation response and Location header
If a request creates a resource, a 201 response can identify the primary created resource via Location. This handler models only the response contract with a fixed synthetic ID; it does not persist data or allocate unique IDs. Standalone synthetic example. Expected stdout (not executed): 201 /items/7 Common mistake: Claiming a resource was persisted merely because the handler returned 201.
Formal Syntax & Structure
rust syntaxrust
POST /items → 201 Created + LocationWorking Example
Example Snippetrust
use axum::{Router, Json, body::{Body, to_bytes}, http::{Request, StatusCode}};use serde::{Deserialize, Serialize};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() { use axum::{routing::post, http::header}; let app = Router::new().route("/items", post(|| async { (StatusCode::CREATED, [(header::LOCATION, "/items/7")], Json(serde_json::json!({"id":7}))) })); let res = app.oneshot(Request::builder().method("POST").uri("/items").body(Body::empty()).unwrap()).await.unwrap(); println!("{} {}", res.status().as_u16(), res.headers()[header::LOCATION].to_str().unwrap());}