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

This commit is contained in:
Robin Stocker
2023-02-25 10:13:32 +00:00
committed by GitHub
parent 27f05ad32e
commit 37e2a7d5e7
2 changed files with 14 additions and 3 deletions
+9 -3
View File
@@ -168,20 +168,26 @@ handler takes.
For example
```rust
use axum::http::{Method, HeaderMap};
use axum::{extract::State, http::{Method, HeaderMap}};
#
# #[derive(Clone)]
# struct AppState {
# }
async fn handler(
// `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,
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
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:
+5
View File
@@ -46,6 +46,11 @@ use std::{
/// # 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
///
/// Multiple [`Router`]s can be combined with [`Router::nest`] or [`Router::merge`]