Skip to content

Commit

Permalink
docs: Clarify that state parameters need to come before body too (#1785)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinst authored Feb 25, 2023
1 parent 27f05ad commit 37e2a7d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 9 additions & 3 deletions axum/src/docs/extract.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions axum/src/extract/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down

0 comments on commit 37e2a7d

Please sign in to comment.