Skip to main content

Response

Struct Response 

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

An outgoing HTTP response.

Implementations§

Source§

impl Response

Source

pub fn new(status: StatusCode) -> Response

An empty response with status.

Source

pub fn ok(body: impl Into<Body>) -> Response

200 OK with body.

Source

pub fn no_content() -> Response

204 No Content.

Source

pub fn text(body: impl Into<String>) -> Response

200 OK with a text/plain body.

Source

pub fn html(body: impl Into<String>) -> Response

200 OK with a text/html body.

Source

pub fn json(value: &impl Serialize) -> Response

200 OK with value serialised as JSON.

A value that cannot be serialised becomes a 500 describing the failure, because there is nowhere else for the error to go — the signature has to produce a response.

Source

pub fn created(location: impl AsRef<str>) -> Response

201 Created, with a Location header.

Source

pub fn download(bytes: impl Into<Bytes>, file_name: &str) -> Response

A response that offers the body as a file download.

Source

pub fn stream<S>(stream: S) -> Response
where S: Stream<Item = Result<Bytes, Error>> + Send + 'static,

A streaming response.

Source

pub fn status(&self) -> StatusCode

The status code.

Source

pub fn headers(&self) -> &HeaderMap

The headers.

Source

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

Mutable headers.

Source

pub fn header(&self, name: &str) -> Option<&str>

One header as a string.

Source

pub fn body(&self) -> &Body

The body.

Source

pub fn take_body(&mut self) -> Body

Take the body, leaving an empty one behind.

Source

pub fn extensions(&self) -> &Extensions

Response attributes, keyed by type.

Source

pub fn extensions_mut(&mut self) -> &mut Extensions

Mutable response attributes.

Source

pub fn is_successful(&self) -> bool

Whether the status is 2xx.

Source

pub fn with_status(self, status: StatusCode) -> Response

Replace the status.

Source

pub fn with_body(self, body: impl Into<Body>) -> Response

Replace the body.

Source

pub fn with_header(self, name: &str, value: &str) -> Response

Set a header, replacing any existing one of that name.

An invalid name or value is dropped rather than panicking: header content often comes from configuration or user data, and taking down a request for it would be worse than omitting it.

Source

pub fn with_added_header(self, name: &str, value: &str) -> Response

Append a header, keeping any existing ones of that name.

Source

pub fn with_content_type(self, content_type: &str) -> Response

Set Content-Type.

Attach a Set-Cookie header. Several cookies may be attached.

Instruct the client to drop a cookie.

Source

pub async fn into_bytes(self) -> Result<Bytes, Error>

The body, collected into bytes.

Consumes the response, because a streaming body can only be read once. For a test, into_string is usually what you want.

Source

pub async fn into_string(self) -> Result<String, Error>

The body, collected and decoded as UTF-8.

The one an assertion reaches for. Without it, reading a body in a test means response.into_http().into_body().collect().await — correct for a server, wrong ergonomics for assert_eq!.

Invalid UTF-8 is replaced rather than refused: a test that is about to print this wants to see what arrived, not an error about it.

Source

pub async fn into_json<T>(self) -> Result<T, Error>

The body, parsed as JSON.

Source

pub fn into_http(self) -> Response<Body>

Convert into an http::Response, ready for the server to write.

Trait Implementations§

Source§

impl Debug for Response

Source§

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

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

impl Default for Response

Source§

fn default() -> Response

Returns the “default value” for a type. Read more
Source§

impl From<Error> for Response

Source§

fn from(error: Error) -> Response

Converts to this type from the input type.
Source§

impl IntoResponse for Response

Source§

fn into_response(self) -> Response

Build the response.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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