HTTP request response model
Quick answer
An HTTP request contains a method, a target, headers and possibly a body. The response has a status code, headers and possibly a body. Rust services usually represent these parts with library types instead of parsing HTTP manually.
Model the contract before writing a server
GET retrieves a representation; POST commonly submits data for processing. Use 201 for a successfully created resource, 400 for malformed requests and 404 for absent resources when appropriate. Content-Type describes the body format; Accept expresses response preferences. HTTPS adds TLS beneath HTTP. These examples construct HTTP objects locally, without sending requests. For Cargo examples use the existing Axum 0.8.9 candidate pin with json and query features, and Tokio 1.44.2 with rt and macros when needed.
use axum::http::Request;fn main() { let r = Request::builder().method("GET").uri("/items?limit=2").body(()).unwrap(); println!("{} {}", r.method(), r.uri().path()); println!("{}", r.uri().query().unwrap());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
GET /itemslimit=2The query is separate from the path.
use axum::http::Response;fn main() { let r = Response::builder().status(201).header("content-type", "application/json").body("{\"id\":1}").unwrap(); println!("{}", r.status().as_u16()); println!("{}", r.body());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
201{"id":1}The status and the JSON body serve different roles.
Common mistake
Do not use HTTP 200 for every error merely because the server returned a response.
Try it yourself
Design the request and response for creating an item, including one validation failure.
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
HTTP request response model — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A request body is JSON, while the client requests a JSON response. Which headers express these separately?
HTTP request response model — 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::http::Request; fn main() { let r = Request::builder().method("GET").uri("/items?limit=2").body(()).unwrap(); println!("{} {}", r.method(), r.uri().path()); println!("{}", r.uri().query().unwrap()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
HTTP request response model — 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::http::Response; fn main() { let r = Response::builder().status(201).header("content-type", "application/json").body("{\"id\":1}").unwrap(); println!("{}", r.status().as_u16()); println!("{}", r.body()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.