I have a requirement that I need to handle a request with multiple ways in a same HTTP route, according to the JSON in the body.
There is a total different JSON object in route root("/")
like
- url_verification
{
"challenge": "b397743d-51c9-40c2-a529-098fd2ff7b4a",
"token": "1p37CgbHAxiVCmzPhY8epfT2rV7YBSQi",
"type": "url_verification"
}
- receive message
{
"schema": "2.0",
"header": {
"event_id": "f7984f25108f8137722bb63cee927e66",
"token": "066zT6pS4QCbgj5Do145GfDbbagCHGgF",
"create_time": "1603977298000000",
"event_type": "contact.user_group.created_v3",
"tenant_key": "xxxxxxx",
"app_id": "cli_xxxxxxxx",
},
"event":{
}
}
Here is my Rust code:
#[derive(Deserialize, Serialize)]
struct UrlVerification {
challenge: String,
token: String,
r#type: String,
}
#[derive(Deserialize, Serialize)]
struct UrlVerificationResp {
challenge: String,
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
let app = Router::new()
.route("/", post(url_verification));
// .route("/", post(msg_recv)); // Overlapping method route
let addr = SocketAddr::from(([127,0,0,1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
// can not receive a parsed json as a params
async fn url_verification(Json(verified): Json<UrlVerification>) -> impl IntoResponse {
(StatusCode::OK, Json(UrlVerificationResp {
challenge: verified.challenge
}))
}
So in Axum, how to solve this kind of problem elegantly?