How to pass State and Form into a handler at the same time? #2531
-
SummaryI'm a Rust newbie and it's the first time I use axum. I would like to receive POST forms and do something like password checking with the app state which stores password hashes. When I pass Form and State at the same time, the compiler shows these message that I cannot understand:
Here's my code in function // prepare users & sessions
let app_state = state::AppState {
users: Arc::new(Mutex::new(user::from_database(&database).await?)),
sessions: Arc::new(Mutex::new(session::from_database(&database).await?)),
};
let app = Router::new()
.route("/login", post(user::handler::api_login))
.with_state(app_state.clone()); The handler #[derive(Serialize)]
#[serde(default, rename_all = "camelCase")]
pub struct ApiLoginResponse {
success: bool,
message: Option<String>,
}
#[derive(Serialize)]
#[serde(default, rename_all = "camelCase")]
pub struct ApiLoginForm {
uid: Option<String>,
password: Option<String>,
}
pub async fn api_login(
State(app_state): State<state::AppState>,
Form(form): Form<ApiLoginForm>,
) -> Result<Response, StatusCode> {
println!(
"Get form\n[uid] {:?}\n[password] {:?}",
form.uid, form.password
);
let response_data = ApiLoginResponse {
success: true,
message: None,
};
Ok(Json(response_data).into_response())
} I tried Dependencies: [dependencies]
axum = "0.7.4"
axum-extra = { version = "0.9.2", features = ["cookie"] }
chrono = "0.4.31"
reqwest = "0.11.22"
serde = "1.0.192"
serde_derive = "1.0.190"
serde_json = "1.0.107"
tokio = { version = "1.33.0", features = ["full"] }
tokio-stream = "0.1.14"
memory-stats = "1.0.0"
tower-http = { version = "0.5.1", features = ["fs"] }
firestore = { version = "0.39.0" } Did I miss any important steps, or am I using a combination of dependencies with incompatible versions? Thanks. axum version0.7.4 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Try https://docs.rs/axum/latest/axum/attr.debug_handler.html |
Beta Was this translation helpful? Give feedback.
Try https://docs.rs/axum/latest/axum/attr.debug_handler.html