Skip to main content

Response

Struct Response 

Source
pub struct Response { /* private fields */ }
Expand description

A response to a request.

Arrives from a request; it is not constructed directly. Reading the body consumes it, so a second read fails. Self::try_clone gets a copy that can be read separately.

Implementations§

Source§

impl Response

Source

pub fn status(&self) -> StatusCode

The response’s status.

Source

pub fn status_text(&self) -> &'static str

The canonical reason phrase for the status, or empty for a code with no well-known one.

Always the canonical phrase. HTTP/1 lets a server send its own, which is not surfaced here; HTTP/2 and HTTP/3 carry none at all.

Source

pub fn ok(&self) -> bool

Whether the status is in the 2xx range.

Source

pub fn headers(&self) -> &HeaderMap

The response’s headers.

Source

pub fn url(&self) -> &Url

The URL the response came from, after any redirects.

Source

pub fn redirected(&self) -> bool

Whether a redirect was followed to reach this response.

Source

pub fn version(&self) -> Version

The HTTP version the response arrived over.

Source

pub fn peer(&self) -> &PeerInformation

The peer that sent the response.

Source

pub fn try_clone(&self) -> Result<Self, FaithError>

Copy the response, so the body can be read twice.

Both copies read the same underlying body, and neither is disturbed by the other having been cloned. Fails if the body has already been read.

Source

pub fn body_used(&self) -> bool

Whether the body has been read, or handed out as a stream.

Source

pub async fn bytes(&self) -> Result<Vec<u8>, FaithError>

Read the whole body.

Consumes it, so a second read fails. A request’s integrity is verified here, once the whole body is in hand.

Source

pub async fn text(&self) -> Result<String, FaithError>

Read the whole body as text.

Decoded as UTF-8, with invalid sequences replaced by U+FFFD.

Source

pub async fn json<T: DeserializeOwned>(&self) -> Result<T, FaithError>

Read the whole body and deserialise it from JSON.

Reads into memory before parsing, which can cost twice the body’s size; Self::body_stream avoids that.

Source

pub fn body_stream(&self) -> Result<Option<BodyReader>, FaithError>

The body as a stream of chunks, decoded under whichever coding was negotiated.

None for a response that cannot carry a body. Callable more than once: every stream a response hands out reads from its one position in the body, so one taken after part of the body was read continues from there. Dropping a stream gives the response’s claim on the body up, stopping the transfer once no clone still wants it. Fails if the body has already been read to the end or discarded.

Source

pub async fn discard(&self)

Give up on the body, releasing the connection back to the pool.

Worth doing when the body is not wanted: left unread, the claim on it is held until the response drops. Gives up this response’s claim only, so a clone still reading carries on; when it was the last claim, resolves once the transfer has been stopped: the stream reset on HTTP/2 and HTTP/3, the connection drained back to the pool or closed on HTTP/1.

Source

pub async fn timing(&self) -> RequestTiming

The timing of the request that produced this response, once its body has ended.

Source

pub async fn trailers(&self) -> Trailers

The trailers, once the body has ended.

A body that is never read never ends, so this waits indefinitely; see Trailers.

Source

pub async fn write_to_file( &self, path: impl AsRef<Path>, options: &FileDestination, on_progress: impl FnMut(FileProgress), ) -> Result<FileWritten, FaithError>

Write the body to a file, reporting progress as the bytes land.

on_progress is called with the bytes written so far and the advertised length when it is known, at most every 50ms, and once more when the last byte is written.

Source§

impl Response

Source

pub fn into_http(self) -> Result<Response<ResponseBody>, FaithError>

Convert into an http::Response, for code written against the wider ecosystem.

Fails if the body is already being consumed elsewhere. A response that cannot carry a body yields an empty one.

Trait Implementations§

Source§

impl Clone for Response

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Response

Source§

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

Formats the value using the given formatter. Read more
Source§

impl TryFrom<Response> for Response<ResponseBody>

Source§

type Error = FaithError

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

fn try_from(response: Response) -> Result<Self, Self::Error>

Performs the conversion.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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