Getting response headers asynchronously by chunk #3758
-
Is there a way to get response headers asynchronously byte by byte (chunk by chunk), the same way we can get response body [1], instead of making a blocking synchronous call [2]? Basically, I'm trying to create a timing utility that returns TTFB (time taken to receive the first byte of response) information for an HTTP request and for that I'd like to get a hook into when the first chunk of response headers is received instead of waiting until all the headers are received which could potentially take multiple reads. [1] // Stream the body, writing each frame to stdout as it arrives
while let Some(next) = res.frame().await {
let frame = next?;
if let Some(chunk) = frame.data_ref() {
io::stdout().write_all(chunk).await?;
}
} [2] // Await the response...
let begin = Instant::now();
let mut res = sender.send_request(req).await?;
let elapsed = begin.elapsed(); // could be more than the actual time to first byte
println!("Response status: {}", res.status());
println!("Response headers: {}", res.headers()); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
There isn't a way to get the headers "by chunks", and I don't expect there will be. However, getting TTFB is a pretty common ask, and is something that we would like to expose in some sort of metrics API. We just don't have one yet, nor even a proposal. If you're interested in working on one, let me know. It will be part of the next roadmap update (though will require a champion). |
Beta Was this translation helpful? Give feedback.
There isn't a way to get the headers "by chunks", and I don't expect there will be. However, getting TTFB is a pretty common ask, and is something that we would like to expose in some sort of metrics API. We just don't have one yet, nor even a proposal. If you're interested in working on one, let me know. It will be part of the next roadmap update (though will require a champion).