pub struct Response {
    pub version: String,
    pub status_code: StatusCode,
    pub headers: Headers,
    pub body: Vec<u8>,
}
Expand description

Represents a response from the server. Implements Into<Vec<u8>> so can be serialised into bytes to transmit.

Simple Creation

Response::new(StatusCode::OK, b"Success")

Advanced Creation

Response::empty(StatusCode::OK)
    .with_bytes(b"Success")
    .with_header(HeaderType::ContentType, "text/plain")

Fields§

§version: String

The HTTP version of the response.

§status_code: StatusCode

The status code of the response, for example 200 OK.

§headers: Headers

A list of the headers included in the response.

§body: Vec<u8>

The body of the response.

Implementations§

source§

impl Response

source

pub fn new<T>(status_code: StatusCode, bytes: T) -> Selfwhere T: AsRef<[u8]>,

Creates a new response object with the given status code, bytes and request. Functionally equivalent to the following (but with some allocation optimisations not shown):

Response::empty(status_code).with_bytes(bytes)
Note about Headers

If you want to add headers to a response, ideally use Response::empty and the builder pattern so as to not accidentally override important generated headers such as content length and connection.

source

pub fn empty(status_code: StatusCode) -> Self

Creates a new response object with the given status code. Automatically sets the HTTP version to “HTTP/1.1”, sets no headers, and creates an empty body.

source

pub fn redirect<T>(location: T) -> Selfwhere T: AsRef<str>,

Creates a redirect response to the given location.

source

pub fn with_header( self, header: impl HeaderLike, value: impl AsRef<str> ) -> Self

Adds the given header to the response. Returns itself for use in a builder pattern.

Adds the given cookie to the response in the Set-Cookie header. Returns itself for use in a builder pattern.

Example
Response::empty(StatusCode::OK)
    .with_bytes(b"Success")
    .with_cookie(
        SetCookie::new("SessionToken", "abc123")
            .with_max_age(Duration::from_secs(3600))
            .with_secure(true)
            .with_path("/")
    )
source

pub fn with_bytes<T>(self, bytes: T) -> Selfwhere T: AsRef<[u8]>,

Appends the given bytes to the body. Returns itself for use in a builder pattern.

source

pub fn get_headers(&self) -> &Headers

Returns a reference to the response’s headers.

source

pub fn text(&self) -> Option<String>

Returns the body as text, if possible.

source

pub fn from_stream<T>(stream: &mut T) -> Result<Self, ResponseError>where T: Read,

Attempts to read and parse one HTTP response from the given stream.

Converts chunked transfer encoding into a regular body.

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 From<Response> for Vec<u8>

source§

fn from(val: Response) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.