Struct Response

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

An HTTP response

Implementations§

Source§

impl Response

Source

pub fn new<S>(status: S) -> Self
where S: TryInto<StatusCode>, S::Error: Debug,

Create a new instance.

Source

pub fn builder<S>(status: S) -> ResponseBuilder
where S: TryInto<StatusCode>, S::Error: Debug,

Begin a chained response builder. For more details, see ResponseBuilder

§Example:
let mut response = Response::builder(203)
    .body("<html>hi</html>")
    .header("custom-header", "value")
    .content_type(mime::HTML)
    .build();

assert_eq!(response.take_body().into_string().await.unwrap(), "<html>hi</html>");
assert_eq!(response.status(), StatusCode::NonAuthoritativeInformation);
assert_eq!(response["custom-header"], "value");
assert_eq!(response["content-type"], "text/html;charset=utf-8");
Source

pub fn status(&self) -> StatusCode

Returns the http status code.

Source

pub fn set_status<S>(&mut self, status: S)
where S: TryInto<StatusCode>, S::Error: Debug,

Set the http status code.

§Example:
let mut response = Response::new(StatusCode::Ok);

response.set_status(418); // the status can be a valid u16 http status code
assert_eq!(response.status(), StatusCode::ImATeapot);

response.set_status(StatusCode::NonAuthoritativeInformation); // or a tide::StatusCode
assert_eq!(response.status(), StatusCode::NonAuthoritativeInformation);
§Panics

set_status will panic if the status argument cannot be successfully converted into a StatusCode.

Response::new(200).set_status(210); // this is not an established status code and will panic
Source

pub fn len(&self) -> Option<usize>

Get the length of the body.

Source

pub fn is_empty(&self) -> Option<bool>

Checks if the body is empty.

Source

pub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues>

Get an HTTP header.

Source

pub fn header_mut( &mut self, name: impl Into<HeaderName>, ) -> Option<&mut HeaderValues>

Get an HTTP header mutably.

Source

pub fn remove_header( &mut self, name: impl Into<HeaderName>, ) -> Option<HeaderValues>

Remove a header.

Source

pub fn insert_header( &mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues, )

Insert an HTTP header.

Source

pub fn append_header( &mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues, )

Append an HTTP header.

Source

pub fn iter(&self) -> Iter<'_>

An iterator visiting all header pairs in arbitrary order.

Source

pub fn iter_mut(&mut self) -> IterMut<'_>

An iterator visiting all header pairs in arbitrary order, with mutable references to the values.

Source

pub fn header_names(&self) -> Names<'_>

An iterator visiting all header names in arbitrary order.

Source

pub fn header_values(&self) -> Values<'_>

An iterator visiting all header values in arbitrary order.

Source

pub fn content_type(&self) -> Option<Mime>

Get the response content type as a Mime.

This gets the request Content-Type header.

Read more on MDN

Source

pub fn set_content_type(&mut self, mime: impl Into<Mime>)

Set the response content type from a MIME.

This sets the response Content-Type header.

Read more on MDN

Source

pub fn set_body(&mut self, body: impl Into<Body>)

Set the body reader.

Source

pub fn take_body(&mut self) -> Body

Take the response body as a Body.

This method can be called after the body has already been taken or read, but will return an empty Body.

Useful for adjusting the whole body, such as in middleware.

Source

pub fn swap_body(&mut self, body: &mut Body)

Swaps the value of the body with another body, without deinitializing either one.

§Examples
use tide::Response;

let mut req = Response::new(200);
req.set_body("Hello, Nori!");

let mut body = "Hello, Chashu!".into();
req.swap_body(&mut body);

let mut string = String::new();
body.read_to_string(&mut string).await?;
assert_eq!(&string, "Hello, Nori!");

Insert cookie in the cookie jar.

Removes the cookie. This instructs the CookiesMiddleware to send a cookie with empty value in the response.

§Warning

Take care when calling this function with a cookie that was returned by Request::cookie. As per section 5.3 step 11 of RFC 6265, a new cookie is only treated as the same as an old one if it has a matching name, domain and path.

The domain and path are not sent to the server on subsequent HTTP requests, so if a cookie was originally set with a domain and/or path, calling this function on a cookie with the same name but with either a different, or no, domain and/or path will lead to us sending an empty cookie that the user agent will treat as unrelated to the original one, and will thus not remove the old one.

To avoid this you can manually set the domain and path as necessary after retrieving the cookie using Request::cookie.

Source

pub fn error(&self) -> Option<&Error>

Returns an optional reference to an error if the response contains one.

Source

pub fn downcast_error<E>(&self) -> Option<&E>
where E: Display + Debug + Send + Sync + 'static,

Returns a reference to the original error associated with this response if there is one and if it can be downcast to the specified type.

§Example
use tide::Response;

let error = std::io::Error::new(ErrorKind::Other, "oh no!");
let error = tide::http::Error::from(error);

let mut res = Response::new(400);
res.set_error(error);

if let Some(err) = res.downcast_error::<std::io::Error>() {
  // Do something with the `std::io::Error`.
}
Source

pub fn take_error(&mut self) -> Option<Error>

Takes the error from the response if one exists, replacing it with None.

Source

pub fn set_error(&mut self, error: impl Into<Error>)

Sets the response’s error, overwriting any existing error.

This is particularly useful for middleware which would like to notify further middleware that an error has occured without overwriting the existing response.

Source

pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T>

Get a response scoped extension value.

Source

pub fn insert_ext<T: Send + Sync + 'static>(&mut self, val: T)

Set a response scoped extension value.

Source

pub fn from_res<T>(value: T) -> Self
where T: Into<Response>,

Create a tide::Response from a type that can be converted into an http_types::Response.

Trait Implementations§

Source§

impl AsMut<Headers> for Response

Source§

fn as_mut(&mut self) -> &mut Headers

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<Response> for Response

Source§

fn as_mut(&mut self) -> &mut Response

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<Headers> for Response

Source§

fn as_ref(&self) -> &Headers

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Response> for Response

Source§

fn as_ref(&self) -> &Response

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for Response

Source§

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

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

impl<'a> From<&'a str> for Response

Source§

fn from(s: &'a str) -> Self

Converts to this type from the input type.
Source§

impl From<Body> for Response

Source§

fn from(body: Body) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Response

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Response> for Response

Source§

fn from(res: Response) -> Self

Converts to this type from the input type.
Source§

impl From<StatusCode> for Response

Source§

fn from(status: StatusCode) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Response

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<Value> for Response

Source§

fn from(json_value: Value) -> Self

Converts to this type from the input type.
Source§

impl Index<&str> for Response

Source§

fn index(&self, name: &str) -> &HeaderValues

Returns a reference to the value corresponding to the supplied name.

§Panics

Panics if the name is not present in Response.

Source§

type Output = HeaderValues

The returned type after indexing.
Source§

impl Index<HeaderName> for Response

Source§

fn index(&self, name: HeaderName) -> &HeaderValues

Returns a reference to the value corresponding to the supplied name.

§Panics

Panics if the name is not present in Response.

Source§

type Output = HeaderValues

The returned type after indexing.
Source§

impl<T: AsRef<str>> Into<Response> for &Redirect<T>

Source§

fn into(self) -> Response

Converts this type into the (usually inferred) input type.
Source§

impl<T: AsRef<str>> Into<Response> for Redirect<T>

Source§

fn into(self) -> Response

Converts this type into the (usually inferred) input type.
Source§

impl<State: Clone + Send + Sync + 'static> Into<Response> for Request<State>

Source§

fn into(self) -> Response

Converts this type into the (usually inferred) input type.
Source§

impl Into<Response> for Response

Source§

fn into(self) -> Response

Converts this type into the (usually inferred) input type.
Source§

impl Into<Response> for ResponseBuilder

Source§

fn into(self) -> Response

Converts this type into the (usually inferred) input type.
Source§

impl<'a> IntoIterator for &'a Response

Source§

type Item = (&'a HeaderName, &'a HeaderValues)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut Response

Source§

type Item = (&'a HeaderName, &'a mut HeaderValues)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Response

Source§

fn into_iter(self) -> Self::IntoIter

Returns a iterator of references over the remaining items.

Source§

type Item = (HeaderName, HeaderValues)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T