expression
Axum: return a status and headers
A handler can return status, headers and body as a tuple implementing IntoResponse. This is convenient for small responses. Do not copy arbitrary user-controlled header names or values. In-memory standalone Cargo example. No socket is opened. Expected stdout (not executed): 202 queued Common mistake: Returning a status code in a text body while leaving the actual HTTP status unchanged.
Formal Syntax & Structure
rust syntaxrust
(StatusCode, [(header, value)], body)Working Example
Example Snippetrust
use axum::{Router, body::{Body, to_bytes}, http::{Request, StatusCode}};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() { use axum::routing::get; let app = Router::new().route("/notice", get(|| async { (StatusCode::ACCEPTED, [("x-stage", "queued")], "accepted") })); let r = app.oneshot(Request::builder().uri("/notice").body(Body::empty()).unwrap()).await.unwrap(); println!("{}", r.status().as_u16()); println!("{}", r.headers()["x-stage"].to_str().unwrap());}