Can anyone please help me to understand, how can I get request details & Json body from handler?
Thanks
E.g.
async fn set_value(
State(app_state): State<Arc<Mutex<AppState>>>,
Json(b): Json<AppState>,
req: Request<Body>,
) -> impl IntoResponse {
// This is an example, there will be multiple condition based on request method or user-agent
if req.method() == Method::GET {
println!("Get Request");
} else {
println!("Post Request");
}
let mut app_state: MutexGuard<AppState> = app_state
.lock()
.map_err(|_| error!("Fail to lock mutex"))
.unwrap();
*app_state = b;
let r: String = serde_json::to_string(&b)
.map_err(|_| error!("Fail to serialize"))
.unwrap();
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.body(r)
.map_err(|_| error!("Fail to build response"))
.unwrap()
}
Error
rror[E0277]: the trait bound `fn(axum::extract::State<Arc<std::sync::Mutex<AppState>>>, Json<AppState>, Request<Body>) -> impl Future<Output = impl IntoResponse> {set_value}: Handler<_, _, _>` is not satisfied
--> src/main.rs:187:50
|
187 | let router = router.route("/set-value", post(set_value));
| ---- ^^^^^^^^^ the trait `Handler<_, _, _>` is not implemented for fn item `fn(axum::extract::State<Arc<std::sync::Mutex<AppState>>>, Json<AppState>, Request<Body>) -> impl Future<Output = impl IntoResponse> {set_value}`
| |
| required by a bound introduced by this call