Skip to main content
In Axum, any type that implements the IntoResponse trait can be returned from a handler. This provides a flexible and type-safe way to construct HTTP responses.

The IntoResponse trait

The IntoResponse trait converts types into HTTP responses:
Many types implement this trait out of the box.

Basic response types

String types return plain text responses:
Returns Content-Type: text/plain; charset=utf-8

JSON responses

Return JSON using the Json wrapper:
Returns Content-Type: application/json

Tuple responses

Combine status codes, headers, and bodies using tuples:

Custom headers

Add headers to responses:

Redirects

Redirect to different URLs:

Result types

Use Result for error handling:
Both the Ok and Err types must implement IntoResponse.

Custom response types

Implement IntoResponse for your own types:

Response builders

For full control, build responses manually:

Streaming responses

Stream events to clients:

Response parts

Modify response parts separately:

Next steps

Error Handling

Learn advanced error handling patterns

Middleware

Transform requests and responses with middleware