mirror of
https://github.com/tokio-rs/axum.git
synced 2025-10-02 15:24:54 +00:00
docs: Clarify that state parameters need to come before body too (#1785)
This commit is contained in:
parent
27f05ad32e
commit
37e2a7d5e7
@ -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:
|
||||||
|
@ -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`]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user