How to know the type of response body? #2420
Answered
by
davidpdrsn
Gumichocopengin8
asked this question in
Q&A
-
SummaryRouter::new()
.route("/api/data/:id", get(get_data))
// ...
// I lose type since return type is `impl IntoResponse`
async fn get_data(Path(id): Path<String>) -> impl IntoResponse {
let data: BodyType = fetch_crate(&id).await;
Json(data).into_response()
}
#[cfg(test)]
mod tests {
#[tokio::test]
async fn should_get_data() {
let app = app();
let response = app
.oneshot(Request::builder().uri("/api/data/test").body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
// I got an error since the type is not correct. How can I get body with the correct type?
let body: BodyType = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(body.data.size, 3); // I wanna test something like this, so I need to know the type but not sure how to do it
}
} if I want to test the response body, how can I know the type of JSON response body? axum version0.7.2 |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Dec 11, 2023
Replies: 1 comment 2 replies
-
http turns everything into bytes. There is no notion of "preserving types across http". There is just serializing and deserializing bytes. You have to say more about the specific error you got. Did you mean to serialize using |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Gumichocopengin8
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http turns everything into bytes. There is no notion of "preserving types across http". There is just serializing and deserializing bytes.
You have to say more about the specific error you got. Did you mean to serialize using
serde_json
?