FromRequestParts or FromRequest trait.
How extractors work
Extractors are types that implementFromRequest or FromRequestParts. When used as handler arguments, Axum automatically extracts the data:
Common extractors
Axum provides many built-in extractors:- Path
- Query
- Json
- Form
- Headers
- State
Extract data from the URL path:
The order of extractors
Extractors are processed in order:FromRequestPartsextractors come first (Path, Query, State, headers, etc.)FromRequestextractors come last (body extractors like Json, Form, Bytes)
Optional extractors
UseOption<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:FromRequestParts
FromRequestParts
Extractors that only need access to the request head (method, URI, headers, extensions):
PathQueryStateHeaderMapMethodUri
FromRequestParts extractors can be used in any order.FromRequest
FromRequest
Extractors that consume the request body:
JsonFormBytesStringRequest
FromRequest extractor can be used per handler, and it must come last (except for Next in middleware).Custom extractors
Create your own extractors by implementingFromRequestParts or FromRequest:
Derive macros
Next steps
State
Learn how to share state across handlers
Responses
Understand how to return different response types