http: add HttpResponse::bytes() for binary downloads

Mirror of text(), but returns raw Bytes without lossy UTF-8 conversion.
Needed by the Telegram channel to fetch photo files.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
Kent Overstreet 2026-05-01 17:58:35 -04:00
commit a075e30557

View file

@ -154,6 +154,14 @@ impl HttpResponse {
Ok(String::from_utf8_lossy(&bytes).into_owned()) Ok(String::from_utf8_lossy(&bytes).into_owned())
} }
/// Read the entire body as raw bytes (for binary downloads).
pub async fn bytes(self) -> Result<Bytes> {
let bytes = self.body.collect().await
.context("reading response body")?
.to_bytes();
Ok(bytes)
}
/// Read the entire body and deserialize as JSON. /// Read the entire body and deserialize as JSON.
pub async fn json<T: serde::de::DeserializeOwned>(self) -> Result<T> { pub async fn json<T: serde::de::DeserializeOwned>(self) -> Result<T> {
let bytes = self.body.collect().await let bytes = self.body.collect().await