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

An empty response with status.

Source

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

200 OK with body.

Source

pub fn no_content() -> Self

204 No Content.

Source

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

200 OK with a text/plain body.

Source

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

200 OK with a text/html body.

Source

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

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

201 Created, with a Location header.

Source

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

A response that offers the body as a file download.

Source

pub fn stream<S>(stream: S) -> Self
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) -> Self

Replace the status.

Source

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

Replace the body.

Source

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

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

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

Source

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

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: DeserializeOwned>(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

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

impl Default for Response

Source§

fn default() -> Self

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

impl From<Error> for Response

Source§

fn from(error: Error) -> Self

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.