Routing and handlers
Quick answer
A router matches a request method and path to a handler. An extractor converts selected request data into typed handler parameters.
Test the route without a listening socket
In Axum 0.8, a path parameter is written as {id}. Path<u32> extracts and parses it; an invalid number is rejected before the handler body runs. Router::oneshot via Tower's ServiceExt sends a request directly through the service. In addition to the prior Axum and Tokio candidates use Tower 0.5.3 with util. No network listener is required. Route-specific business logic belongs in separate functions when it grows.
use axum::{Router, routing::get, extract::Path, body::Body, http::Request};use tower::ServiceExt;async fn item(Path(id): Path<u32>) -> String { format!("item {id}") }#[tokio::main(flavor = "current_thread")]async fn main() { let app = Router::new().route("/items/{id}", get(item)); let res = app.oneshot(Request::builder().uri("/items/7").body(Body::empty()).unwrap()).await.unwrap(); println!("{}", res.status().as_u16());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
200The path value is extracted into the handler.
use axum::{Router, routing::get, extract::Path, body::Body, http::Request};use tower::ServiceExt;async fn item(Path(id): Path<u32>) -> String { id.to_string() }#[tokio::main(flavor = "current_thread")]async fn main() { let app = Router::new().route("/items/{id}", get(item)); let res = app.oneshot(Request::builder().uri("/items/no").body(Body::empty()).unwrap()).await.unwrap(); println!("{}", res.status().as_u16());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
400The extractor fails before application lookup.
Common mistake
Do not confuse malformed IDs with valid IDs that are not found in storage.
Try it yourself
Add a route with no parameters and test a missing path for 404.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Routing and handlers — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A route extracts Path<u32> but receives a nonnumeric ID. What happens before the handler body?
Routing and handlers — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use axum::{Router, routing::get, extract::Path, body::Body, http::Request}; use tower::ServiceExt; async fn item(Path(id): Path<u32>) -> String { format!("item {id}") } #[tokio::main(flavor = "current_thread")] async fn main() { let app = Router::new().route("/items/{id}", get(item)); let res = app.oneshot(Request::builder().uri("/items/7").body(Body::empty()).unwrap()).await.unwrap(); println!("{}", res.status().as_u16()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Routing and handlers — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use axum::{Router, routing::get, extract::Path, body::Body, http::Request}; use tower::ServiceExt; async fn item(Path(id): Path<u32>) -> String { id.to_string() } #[tokio::main(flavor = "current_thread")] async fn main() { let app = Router::new().route("/items/{id}", get(item)); let res = app.oneshot(Request::builder().uri("/items/no").body(Body::empty()).unwrap()).await.unwrap(); println!("{}", res.status().as_u16()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.