Skip to main content
Extractors allow you to pull data from HTTP requests in a type-safe way. They implement either the FromRequestParts or FromRequest trait.

How extractors work

Extractors are types that implement FromRequest or FromRequestParts. When used as handler arguments, Axum automatically extracts the data:

Common extractors

Axum provides many built-in extractors:
Extract data from the URL path:

The order of extractors

The order of extractors in your handler signature matters!
Extractors are processed in order:
  1. FromRequestParts extractors come first (Path, Query, State, headers, etc.)
  2. FromRequest extractors come last (body extractors like Json, Form, Bytes)

Optional extractors

Use Option<T> to make an extractor optional:

The Request extractor

Extract the entire request:
Request is a FromRequest extractor, so it must come last (or second-to-last if using Next in middleware).

FromRequest vs FromRequestParts

Understanding the difference:
Extractors that only need access to the request head (method, URI, headers, extensions):
  • Path
  • Query
  • State
  • HeaderMap
  • Method
  • Uri
Multiple FromRequestParts extractors can be used in any order.
Extractors that consume the request body:
  • Json
  • Form
  • Bytes
  • String
  • Request
Only one FromRequest extractor can be used per handler, and it must come last (except for Next in middleware).

Custom extractors

Create your own extractors by implementing FromRequestParts or FromRequest:

Derive macros

Use the FromRequest derive macro to create custom extractors more easily:

Next steps

State

Learn how to share state across handlers

Responses

Understand how to return different response types