Why Response::bytes consume self?
#2984
Unanswered
StackOverflowExcept1on
asked this question in
Q&A
Replies: 1 comment
|
HTTP response bodies are streams - once you read through them, they're gone. There's no way to rewind a network socket, so For your logging use case, just read the bytes first and then work with those: let response = client.get(&url).send().await?;
let status = response.status();
let headers = response.headers().clone();
let bytes = response.bytes().await?;
// log whatever you need
if status == StatusCode::TOO_MANY_REQUESTS {
let body_str = String::from_utf8_lossy(&bytes);
tracing::warn!("Rate limited: {}", body_str);
}
// deserialize from the bytes you already have
let data: MyType = serde_json::from_slice(&bytes)?;The |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Let's say you want to set up logging (in case of some rate limits), but it's not possible or you'll have to manually copy implementation of
json()method.https://docs.rs/reqwest/latest/reqwest/struct.Response.html#method.json

All reactions