how to handle the situation of multiple json parsed for the same route #1780
Answered
by
davidpdrsn
Dawson-Jones
asked this question in
Q&A
-
I'm new to axum. There is a total different json object in route root("/") like
{
"challenge": "b397743d-51c9-40c2-a529-098fd2ff7b4a",
"token": "1p37CgbHAxiVCmzPhY8epfT2rV7YBSQi",
"type": "url_verification"
}
{
"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 copy from readme #[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 |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Feb 22, 2023
Replies: 1 comment
-
Have a look at https://serde.rs/enum-representations.html for the different options. It's more about serde than axum. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Dawson-Jones
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have a look at https://serde.rs/enum-representations.html for the different options. It's more about serde than axum.