Responses - Axum - Complete Documentation by ntxm.org

IntoResponse trait

Understand how Axum turns handler return values into HTTP responses using the IntoResponse trait, with beginner-friendly examples and common implementation patterns

Returning &'static str Responses in Axum

Learn how to return static string literals from Axum handlers using the &'static str type, why it works, common pitfalls, and complete examples for beginners.

String response

Learn how to return a String from an Axum handler, how it becomes an HTTP response, and when to use String over static string slices.

Returning Status Codes Directly in Axum

Learn how to return HTTP status codes like 200 OK and 404 Not Found directly from Axum handlers, how status codes work with the IntoResponse trait, and how to build dynamic status code logic step by step.

HeaderMap Response in Axum

Learn how to return HTTP response headers from Axum handlers using HeaderMap, including creating headers, inserting values, and avoiding common pitfalls.

Response Tuples in Axum Handlers

Learn how to combine status codes, headers, and body into a single response using tuples in Axum handlers - a beginner-friendly guide with many examples

Returning a Status Code and Text Body with (StatusCode, String)

Learn how to return an HTTP status code together with a plain text message from Axum handlers using the (StatusCode, String) tuple, when to use it, and common mistakes beginners make.

Returning Status, Headers, and Body Together

Learn how to build a complete HTTP response in Axum using the (StatusCode, HeaderMap, Body) tuple, including status codes, custom headers, body content, common mistakes, and practical examples.

Json wrapper

Learn how to return JSON responses in Axum using the Json wrapper type and what it does automatically for you.

Html wrapper

Learn how to return HTML responses in Axum using the Html wrapper, set the correct Content-Type header, and avoid common pitfalls such as XSS and incorrect content types.

Understanding Redirect Responses in Axum

Learn how to use Axum's built-in Redirect response type to send users from one URL to another, covering temporary and permanent redirects with complete beginner-friendly examples.

No Content response

Learn how to return a 204 No Content response in Axum using the NoContent type or StatusCode, why 204 is useful, and common mistakes to avoid.

serde Serialize for JSON Responses in Axum

A beginner-friendly guide to the serde Serialize trait, how it converts Rust data into JSON strings, and how Axum uses it to return JSON responses.

Json<T> in Axum Handlers

Learn how to return JSON data from Axum handlers using the Json type and how to parse incoming JSON request bodies with the same reusable type.

Content-Type application/json in Axum Responses

Understand how Axum sets the Content-Type application/json header for JSON responses and how to set it manually when returning raw JSON strings or building custom responses

User struct

Learn how to define a custom User struct in Rust and return it as a JSON response using Axum's Json wrapper and serde Serialize.

Organizing the User Struct in models/user.rs

Learn how to move your User struct into a dedicated models/user.rs file for cleaner organization and reusable JSON responses in Axum applications.

JSON field naming in Axum responses

Learn how serde controls JSON field names in Axum - why Rust struct fields become snake_case keys by default, how to rename them to camelCase or other conventions, and how to avoid broken API clients.

Response::builder in Axum

Learn how to manually construct HTTP responses in Axum using Response::builder, with examples for status codes, headers, and bodies.

Custom response headers in Axum

Learn how to add custom HTTP headers to Axum handler responses using tuples, HeaderMap, and Response builder, with examples and common mistakes explained for beginners.

Redirect::to

How to create temporary redirect responses in Axum using Redirect::to, with detailed examples, explanations, and common pitfalls.

Redirect::permanent

Learn how to send permanent redirects in Axum using the Redirect::permanent method, understand the 308 status code, and avoid common browser caching pitfalls.

Empty Unit () Response

How returning the unit type from an Axum handler produces an empty 200 OK response with no body, including examples, use cases, and common mistakes.

Returning Result from handlers

Learn how to return Rust's Result type from Axum handlers, use the question mark operator for clean error handling, and convert errors into HTTP responses.

Streaming Body Preview

Learn how to return a streaming response body from an Axum handler using Body::from_stream, with examples and common pitfalls.