Skip to main content

Response

Struct Response 

Source
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

Source

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.

Source

pub fn status(&self) -> StatusCode

Returns the HTTP status code.

Source

pub fn version(&self) -> Version

Returns the HTTP version.

Source

pub fn headers(&self) -> &HeaderMap

Returns the response headers.

Source

pub fn headers_mut(&mut self) -> &mut HeaderMap

Returns a mutable reference to the response headers.

Source

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.

Source

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.

Source

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.

Source

pub fn url(&self) -> &Url

Returns the final URL of the response (after any redirects).

Source

pub fn history(&self) -> &[HistoryEntry]

Returns the redirect history (prior responses in order).

Source

pub fn history_mut(&mut self) -> &mut Vec<HistoryEntry>

Returns a mutable reference to the redirect history.

Source

pub fn is_success(&self) -> bool

Returns true if the status code indicates success (2xx).

Source

pub fn body(&self) -> &ResponseBody

Returns a reference to the response body.

Source

pub fn body_mut(&mut self) -> &mut ResponseBody

Returns a mutable reference to the response body.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn network_stream_mut(&mut self) -> Option<&mut NetworkStream>

Returns a mutable reference to the network stream handle.

Source

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.

Source

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).

Trait Implementations§

Source§

impl Debug for Response

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more