docs: Clarify that state parameters need to come before body too (#1785)

This commit is contained in:
Robin Stocker 2023-02-25 21:13:32 +11:00 committed by GitHub
parent 27f05ad32e
commit 37e2a7d5e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -168,20 +168,26 @@ handler takes.
For example For example
```rust ```rust
use axum::http::{Method, HeaderMap}; use axum::{extract::State, http::{Method, HeaderMap}};
#
# #[derive(Clone)]
# struct AppState {
# }
async fn handler( async fn handler(
// `Method` and `HeaderMap` don't consume the request body so they can // `Method` and `HeaderMap` don't consume the request body so they can
// put anywhere in the argument list // put anywhere in the argument list (but before `body`)
method: Method, method: Method,
headers: HeaderMap, headers: HeaderMap,
// `State` is also an extractor so it needs to be before `body`
State(state): State<AppState>,
// `String` consumes the request body and thus must be the last extractor // `String` consumes the request body and thus must be the last extractor
body: String, body: String,
) { ) {
// ... // ...
} }
# #
# let _: axum::routing::MethodRouter = axum::routing::get(handler); # let _: axum::routing::MethodRouter<AppState, String> = axum::routing::get(handler);
``` ```
We get a compile error if `String` isn't the last extractor: We get a compile error if `String` isn't the last extractor:

View File

@ -46,6 +46,11 @@ use std::{
/// # let _: axum::Router = app; /// # let _: axum::Router = app;
/// ``` /// ```
/// ///
/// Note that `State` is an extractor, so be sure to put it before any body
/// extractors, see ["the order of extractors"][order-of-extractors].
///
/// [order-of-extractors]: crate::extract#the-order-of-extractors
///
/// ## Combining stateful routers /// ## Combining stateful routers
/// ///
/// Multiple [`Router`]s can be combined with [`Router::nest`] or [`Router::merge`] /// Multiple [`Router`]s can be combined with [`Router::nest`] or [`Router::merge`]