pub struct Response { /* private fields */ }Expand description
An HTTP response.
§Trailers
HTTP trailers (HTTP/1.1 chunked trailers, HTTP/2 trailing HEADERS, and
HTTP/3 trailing headers where the h3 crate exposes them) are captured
without buffering the body. The body stream yields data frames exactly
as before; when the trailing-headers frame arrives it is stored and the
stream ends normally. Use Self::trailers after fully consuming the
body (bytes(), bytes_stream() to EOF) to retrieve them.
Lifecycle: trailers() returns None until the body has advanced far
enough for trailers to arrive, when the response carried no trailers,
or when a body error occurred before trailers (errors remain body
errors, never fabricated trailer state). Partial consumption or early
drop never reports trailers and releases the pool permit without
waiting for them. Read timeouts apply while waiting for trailers at
the body boundary; the explicit native total deadline bounds
transport setup and is not restarted per trailer wait.
Implementations§
Source§impl Response
impl Response
Sourcepub fn trailers(&self) -> Option<HeaderMap>
pub fn trailers(&self) -> Option<HeaderMap>
Returns received HTTP trailers, if any.
Returns None until the body has been fully consumed and trailers
have arrived, when the response carried no trailers, or when a body
error occurred before trailers. Duplicate/multi-value trailing
headers are preserved in the returned HeaderMap.
This never buffers the body: drive the body to EOF via bytes() or
bytes_stream() first, then call this method.
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Returns the HTTP status code.
Sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Returns a mutable reference to the response headers.
Sourcepub fn wire_content_encoding(&self) -> Option<&str>
pub fn wire_content_encoding(&self) -> Option<&str>
Returns the original wire Content-Encoding value, if present.
Automatic decompression may remove this value from Self::headers;
compatibility adapters can use this read-only snapshot to preserve
wire metadata without changing the core decoded-header policy.
Sourcepub fn wire_content_length(&self) -> Option<&str>
pub fn wire_content_length(&self) -> Option<&str>
Returns the original wire Content-Length value, if present.
The value describes encoded wire bytes and is never derived from the decompressed response body.
Sourcepub fn wire_reason_phrase(&self) -> Option<&str>
pub fn wire_reason_phrase(&self) -> Option<&str>
Returns the original wire reason phrase, if present.
HTTP/1.x responses include a reason phrase after the status code. This preserves the original wire bytes rather than deriving from the status code alone. HTTP/2 has no wire reason phrase.
Sourcepub fn history(&self) -> &[HistoryEntry]
pub fn history(&self) -> &[HistoryEntry]
Returns the redirect history (prior responses in order).
Sourcepub fn history_mut(&mut self) -> &mut Vec<HistoryEntry>
pub fn history_mut(&mut self) -> &mut Vec<HistoryEntry>
Returns a mutable reference to the redirect history.
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Returns true if the status code indicates success (2xx).
Sourcepub fn body(&self) -> &ResponseBody
pub fn body(&self) -> &ResponseBody
Returns a reference to the response body.
Sourcepub fn body_mut(&mut self) -> &mut ResponseBody
pub fn body_mut(&mut self) -> &mut ResponseBody
Returns a mutable reference to the response body.
Sourcepub async fn bytes(&mut self) -> Result<Bytes>
pub async fn bytes(&mut self) -> Result<Bytes>
Consume the response and return the body bytes.
For buffered bodies, this is O(1). For streaming bodies, this collects all chunks.
§Errors
Returns an error if the body has already been consumed or if a stream chunk fails.
Sourcepub async fn text(&mut self) -> Result<String>
pub async fn text(&mut self) -> Result<String>
Consume the response and return the body as a UTF-8 string.
§Errors
Returns an error if the body is not valid UTF-8 or has already been consumed.
Sourcepub fn bytes_stream(&mut self) -> Result<BoxBytesStream>
pub fn bytes_stream(&mut self) -> Result<BoxBytesStream>
Consume the response body and return a stream of byte chunks.
For buffered bodies, yields the entire body as a single chunk. For streaming bodies, yields chunks incrementally.
§Errors
Returns an error if the body has already been consumed.
Sourcepub fn raw_bytes_stream(&mut self) -> Result<BoxBytesStream>
pub fn raw_bytes_stream(&mut self) -> Result<BoxBytesStream>
Consume the response body and return encoded byte chunks.
Compressed streaming responses select the original encoded transport body. Raw and decoded body selection are mutually exclusive and single-consumption operations.
§Errors
Returns an error if the response body has already been consumed.
Sourcepub fn text_lines(&mut self) -> Result<impl Stream<Item = Result<String>>>
pub fn text_lines(&mut self) -> Result<impl Stream<Item = Result<String>>>
Consume the response body and return a stream of text lines.
Each line is decoded as UTF-8 without the trailing newline. Incomplete lines at chunk boundaries are buffered until the next chunk completes them.
§Errors
Returns an error if the body has already been consumed or if a chunk contains invalid UTF-8.
Sourcepub fn network_stream(&self) -> Option<&NetworkStream>
pub fn network_stream(&self) -> Option<&NetworkStream>
Returns a reference to the network stream handle, if available.
For upgraded connections (101/CONNECT), this provides full IO access. For ordinary pooled connections, this provides read-only metadata.
Sourcepub fn network_stream_mut(&mut self) -> Option<&mut NetworkStream>
pub fn network_stream_mut(&mut self) -> Option<&mut NetworkStream>
Returns a mutable reference to the network stream handle.
Sourcepub fn take_network_stream(&mut self) -> Option<NetworkStream>
pub fn take_network_stream(&mut self) -> Option<NetworkStream>
Take the network stream out of the response, leaving None in
its place. This is the canonical way to obtain ownership of an
upgraded stream while still keeping the rest of the response.
Sourcepub fn into_network_stream(self) -> Option<NetworkStream>
pub fn into_network_stream(self) -> Option<NetworkStream>
Consume the response and return the network stream, if any.
This is the primary way to obtain ownership of an upgraded stream (101 Switching Protocols or successful CONNECT).