[][src]Struct tide::http::Response

pub struct Response { /* fields omitted */ }

An HTTP response.

Examples

use http_types::{Response, StatusCode};

let mut res = Response::new(StatusCode::Ok);
res.set_body("Hello, Nori!");

Methods

impl Response[src]

pub fn new(status: StatusCode) -> Response[src]

Create a new response.

pub fn status(&self) -> StatusCode[src]

Get the status

pub fn header_mut(&mut self, name: &HeaderName) -> Option<&mut Vec<HeaderValue>>[src]

Get a mutable reference to a header.

pub fn header(&self, name: &HeaderName) -> Option<&Vec<HeaderValue>>[src]

Get an HTTP header.

pub fn remove_header(&mut self, name: &HeaderName) -> Option<Vec<HeaderValue>>[src]

Remove a header.

pub fn insert_header(
    &mut self,
    name: impl TryInto<HeaderName>,
    values: impl ToHeaderValues
) -> Result<Option<Vec<HeaderValue>>, Error>
[src]

Set an HTTP header.

Examples

use http_types::{Url, Method, Response, StatusCode};

let mut req = Response::new(StatusCode::Ok);
req.insert_header("Content-Type", "text/plain")?;

pub fn append_header(
    &mut self,
    name: impl TryInto<HeaderName>,
    values: impl ToHeaderValues
) -> Result<(), Error>
[src]

Append a header to the headers.

Unlike insert this function will not override the contents of a header, but insert a header if there aren't any. Or else append to the existing list of headers.

Examples

use http_types::{Response, StatusCode};

let mut res = Response::new(StatusCode::Ok);
res.append_header("Content-Type", "text/plain")?;

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

Set the body reader.

Examples

use http_types::{Response, StatusCode};

let mut res = Response::new(StatusCode::Ok);
res.set_body("Hello, Nori!");

pub fn replace_body(&mut self, body: impl Into<Body>) -> Body[src]

Replace the response body with a new body, returning the old body.

Examples

use http_types::{Body, Url, Method, Response, StatusCode};

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

let mut body: Body = req.replace_body("Hello, Chashu");

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

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

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

Examples

use http_types::{Body, Url, Method, Response, StatusCode};

let mut req = Response::new(StatusCode::Ok);
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!");

pub fn take_body(&mut self) -> Body[src]

Take the response body, replacing it with an empty body.

Examples

use http_types::{Body, Url, Method, Response, StatusCode};

let mut req = Response::new(StatusCode::Ok);
req.set_body("Hello, Nori!");
let mut body: Body = req.take_body();

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

pub async fn body_string(self) -> Result<String, Error>[src]

Read the body as a string.

This consumes the response. If you want to read the body without consuming the response, consider using the take_body method and then calling Body::into_string or using the Response's AsyncRead implementation to read the body.

Examples

use http_types::{Body, Url, Method, Response, StatusCode};    
use async_std::io::Cursor;

let mut resp = Response::new(StatusCode::Ok);    
let cursor = Cursor::new("Hello Nori");
let body = Body::from_reader(cursor, None);
resp.set_body(body);
assert_eq!(&resp.body_string().await.unwrap(), "Hello Nori");

pub fn set_content_type(&mut self, mime: Mime) -> Option<Vec<HeaderValue>>[src]

Set the response MIME.

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

Get the length of the body stream, if it has been set.

This value is set when passing a fixed-size object into as the body. E.g. a string, or a buffer. Consumers of this API should check this value to decide whether to use Chunked encoding, or set the response length.

pub fn version(&self) -> Option<Version>[src]

Get the HTTP version, if one has been set.

Examples

use http_types::{Response, StatusCode, Version};

let mut res = Response::new(StatusCode::Ok);
assert_eq!(res.version(), None);

res.set_version(Some(Version::Http2_0));
assert_eq!(res.version(), Some(Version::Http2_0));

pub fn set_version(&mut self, version: Option<Version>)[src]

Set the HTTP version.

Examples

use http_types::{Response, StatusCode, Version};

let mut res = Response::new(StatusCode::Ok);
res.set_version(Some(Version::Http2_0));

pub fn set_status(&mut self, status: StatusCode)[src]

Set the status.

pub fn cookies(&self) -> Result<Vec<Cookie>, Error>[src]

Get all cookies.

Examples

use http_types::{Cookie, Response, StatusCode, Version};

let mut res = Response::new(StatusCode::Ok);
res.set_cookie(Cookie::new("name", "value"));
assert_eq!(res.cookies().unwrap(), vec![Cookie::new("name", "value")]);

pub fn cookie(&self, name: &str) -> Result<Option<Cookie>, Error>[src]

Get a cookie by name.

Examples

use http_types::{Cookie, Response, StatusCode, Version};

let mut res = Response::new(StatusCode::Ok);
res.set_cookie(Cookie::new("name", "value"));
assert_eq!(res.cookie("name").unwrap(), Some(Cookie::new("name", "value")));

Set a cookie.

This will not override any existing cookies, and uses the Cookies header.

Examples

use http_types::{Cookie, Response, StatusCode, Version};

let mut res = Response::new(StatusCode::Ok);
res.set_cookie(Cookie::new("name", "value"));

pub fn send_trailers(&mut self) -> TrailersSender[src]

Sends trailers to the a receiver.

pub async fn recv_trailers(&'_ self) -> Option<Result<Trailers, Error>>[src]

Receive trailers from a sender.

pub fn iter(&'a self) -> Iter<'a>[src]

An iterator visiting all header pairs in arbitrary order.

pub fn iter_mut(&'a mut self) -> IterMut<'a>[src]

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

pub fn header_names(&'a self) -> Names<'a>[src]

An iterator visiting all header names in arbitrary order.

pub fn header_values(&'a self) -> Values<'a>[src]

An iterator visiting all header values in arbitrary order.

pub fn local(&self) -> &TypeMap[src]

Returns a reference to the existing local.

pub fn local_mut(&mut self) -> &mut TypeMap[src]

Returns a mutuable reference to the existing local state.

Examples

use http_types::{StatusCode, Response, Version};

let mut res = Response::new(StatusCode::Ok);
res.local_mut().insert("hello from the extension");
assert_eq!(res.local().get(), Some(&"hello from the extension"));

Trait Implementations

impl AsMut<Headers> for Response[src]

impl AsRef<Headers> for Response[src]

impl AsyncBufRead for Response[src]

impl AsyncRead for Response[src]

impl Debug for Response[src]

impl<'a> From<&'a str> for Response[src]

impl From<Response<Body>> for Response[src]

impl From<Response> for Body[src]

impl From<Response> for Response[src]

impl From<String> for Response[src]

impl From<Vec<u8>> for Response[src]

impl Into<Response> for Response[src]

impl<'a> IntoIterator for &'a Response[src]

type Item = (&'a HeaderName, &'a Vec<HeaderValue>)

The type of the elements being iterated over.

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?

impl IntoIterator for Response[src]

type Item = (HeaderName, Vec<HeaderValue>)

The type of the elements being iterated over.

type IntoIter = IntoIter

Which kind of iterator are we turning this into?

fn into_iter(self) -> <Response as IntoIterator>::IntoIter[src]

Returns a iterator of references over the remaining items.

impl<'a> IntoIterator for &'a mut Response[src]

type Item = (&'a HeaderName, &'a mut Vec<HeaderValue>)

The type of the elements being iterated over.

type IntoIter = IterMut<'a>

Which kind of iterator are we turning this into?

impl<'__pin> Unpin for Response where
    __Origin<'__pin>: Unpin
[src]

Auto Trait Implementations

impl !RefUnwindSafe for Response

impl Send for Response

impl Sync for Response

impl !UnwindSafe for Response

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<R> AsyncBufReadExt for R where
    R: AsyncBufRead + ?Sized
[src]

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> BufReadExt for T where
    T: AsyncBufRead + ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> ReadExt for T where
    T: AsyncRead + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.