pub struct HttpResponse { /* private fields */ }Expand description
HTTP response wrapper with body-reading helpers
Provides a reqwest-like API for reading response bodies:
resp.error_for_status()?- Check status without reading bodyresp.bytes().await?- Read raw bytesresp.checked_bytes().await?- Read bytes with status checkresp.json::<T>().await?- Parse as JSON with status check
All body reads enforce the configured max_body_size limit.
Implementations§
Source§impl HttpResponse
impl HttpResponse
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Get the response status code
Sourcepub fn into_inner(self) -> Response<ResponseBody>
pub fn into_inner(self) -> Response<ResponseBody>
Consume the wrapper and return the inner response with boxed body
Useful for advanced callers who need direct access to the response. Note: The body has already been through the decompression layer, so it contains decompressed bytes if the server sent compressed data.
Sourcepub fn error_for_status(self) -> Result<Self, HttpError>
pub fn error_for_status(self) -> Result<Self, HttpError>
Check status and return error for non-2xx responses
Does NOT read the response body. For non-2xx status, returns
HttpError::HttpStatus with an empty body preview.
§Errors
Returns HttpError::HttpStatus if the response status is not 2xx.
§Example
let resp = client.get("https://example.com/api").send().await?;
let resp = resp.error_for_status()?; // Fails if not 2xx
let body = resp.bytes().await?;Sourcepub async fn bytes(self) -> Result<Bytes, HttpError>
pub async fn bytes(self) -> Result<Bytes, HttpError>
Read response body as bytes without status check
Enforces max_body_size limit.
§Errors
Returns HttpError::BodyTooLarge if body exceeds limit.
Sourcepub async fn checked_bytes(self) -> Result<Bytes, HttpError>
pub async fn checked_bytes(self) -> Result<Bytes, HttpError>
Read response body as bytes with status check
Returns HttpError::HttpStatus for non-2xx responses (with body preview).
Enforces max_body_size limit for successful responses.
§Errors
Returns HttpError::HttpStatus if status is not 2xx.
Returns HttpError::BodyTooLarge if body exceeds limit.
Sourcepub async fn json<T: DeserializeOwned>(self) -> Result<T, HttpError>
pub async fn json<T: DeserializeOwned>(self) -> Result<T, HttpError>
Parse response body as JSON with status check
Equivalent to resp.checked_bytes().await? followed by JSON parsing.
§Errors
Returns HttpError::HttpStatus if status is not 2xx.
Returns HttpError::BodyTooLarge if body exceeds limit.
Returns HttpError::Json if parsing fails.
Sourcepub async fn text(self) -> Result<String, HttpError>
pub async fn text(self) -> Result<String, HttpError>
Read response body as text (UTF-8) with status check
Equivalent to resp.checked_bytes().await? followed by UTF-8 conversion.
Invalid UTF-8 sequences are replaced with the Unicode replacement character.
§Errors
Returns HttpError::HttpStatus if status is not 2xx.
Returns HttpError::BodyTooLarge if body exceeds limit.
§Example
let body = client
.get("https://example.com/text")
.send()
.await?
.text()
.await?;
println!("Response: {}", body);Sourcepub fn into_body(self) -> ResponseBody
pub fn into_body(self) -> ResponseBody
Returns the response body as a stream for incremental processing.
§Warning: No Size Limit Enforcement
This method does NOT enforce max_body_size. For untrusted responses
(especially compressed), prefer into_limited_body()
to protect against decompression bombs and memory exhaustion.
Unlike bytes(), json(), or text(), this method does NOT:
- Check the HTTP status code (use
error_for_status()first if needed) - Enforce the
max_body_sizelimit (caller is responsible for limiting) - Buffer the entire body in memory
Use this only when:
- You trust the response source AND have external size limits
- You’re implementing custom streaming logic with your own limits
- Performance is critical and you can guarantee bounded responses
§Example
use http_body_util::BodyExt;
let response = client.get("https://example.com/large-file").send().await?;
// Check status first (optional)
if !response.status().is_success() {
return Err(/* handle error */);
}
// Get the body stream (WARNING: no size limit!)
let mut body = response.into_body();
// Process frames incrementally
while let Some(frame) = body.frame().await {
let frame = frame?;
if let Some(chunk) = frame.data_ref() {
process_chunk(chunk);
}
}Sourcepub fn into_limited_body(self) -> LimitedBody
pub fn into_limited_body(self) -> LimitedBody
Returns the response body as a size-limited stream.
Unlike into_body(), this method wraps the body in a
LimitedBody that enforces the configured max_body_size limit during
streaming. This protects against decompression bombs where a small compressed
payload expands to gigabytes of memory.
The limit is enforced on decompressed bytes, so a 1KB gzip payload that decompresses to 1GB will be rejected.
§Errors
When the limit is exceeded, the next poll_frame() call returns
HttpError::BodyTooLarge.
§Example
use http_body_util::BodyExt;
let response = client.get("https://example.com/large-file").send().await?;
let mut body = response.into_limited_body();
while let Some(frame) = body.frame().await {
let frame = frame?; // Returns BodyTooLarge if limit exceeded
if let Some(chunk) = frame.data_ref() {
process_chunk(chunk);
}
}
println!("Total bytes read: {}", body.bytes_read());Sourcepub fn max_body_size(&self) -> usize
pub fn max_body_size(&self) -> usize
Returns the configured max body size for this response.
This is the limit that would be applied by bytes(), checked_bytes(),
json(), and text() methods.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for HttpResponse
impl !RefUnwindSafe for HttpResponse
impl Send for HttpResponse
impl Sync for HttpResponse
impl Unpin for HttpResponse
impl !UnwindSafe for HttpResponse
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn decompression(self) -> Decompression<Self>where
Self: Sized,
fn decompression(self) -> Decompression<Self>where
Self: Sized,
Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
Source§fn follow_redirects(self) -> FollowRedirect<Self>where
Self: Sized,
fn follow_redirects(self) -> FollowRedirect<Self>where
Self: Sized,
Source§fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
Source§fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
x-request-id as the header name. Read moreSource§fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
Source§fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
x-request-id as the header name. Read moreSource§fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>where
Self: Sized,
fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>where
Self: Sized,
413 Payload Too Large responses. Read more