Skip to main content

Response

Struct Response 

Source
pub struct Response {
    pub status: Status,
    pub headers: Headers,
    pub body: Vec<u8>,
}
Expand description

HTTP response.

While all members of this struct are public, there are also some dedicated methods with identical names, providing a builder-like interface. However, before creating a response using this struct directly, consider using the ResponseExt trait, which provides several convenient constructors.

§Examples

use zense::http::{Header, Response, Status};

// Create response
let res = Response::new()
    .status(Status::Ok)
    .header(Header::ContentType, "text/plain")
    .header(Header::ContentLength, 11)
    .body("Hello world");

Fields§

§status: Status

Response status.

§headers: Headers

Response headers.

§body: Vec<u8>

Response body.

Implementations§

Source§

impl Response

Source

pub fn new() -> Self

Creates a response.

§Examples
use zense::http::Response;

// Create response
let res = Response::new();
Source

pub fn into_bytes(self) -> Vec<u8>

Converts the response into bytes.

§Examples
use zense::http::{Header, Response, Status};

// Create response
let res = Response::new()
   .status(Status::Ok)
   .header(Header::ContentType, "text/plain")
   .header(Header::ContentLength, 11)
   .body("Hello world");

// Convert response into bytes
let bytes = res.into_bytes();
Source§

impl Response

Source

pub fn status(self, status: Status) -> Self

Sets the status of the response.

§Examples
use zense::http::{Response, Status};

// Create response and set status
let res = Response::new()
    .status(Status::Ok);
Source

pub fn header<V>(self, header: Header, value: V) -> Self
where V: ToString,

Adds a header to the response.

§Examples
use zense::http::{Header, Response};

// Create response and add header
let res = Response::new()
    .header(Header::ContentType, "text/plain");
Source

pub fn body<B>(self, body: B) -> Self
where B: Into<Vec<u8>>,

Sets the body of the response.

Warning: Albeit the Header::ContentLength header is required in most cases, it’s not automatically set when using this method, since it belongs to the low-level Response interface. Please consider using the much more convenient ResponseExt methods.

§Examples
use zense::http::Response;

// Create response and set body
let res = Response::new()
    .body("Hello world");

Trait Implementations§

Source§

impl Clone for Response

Source§

fn clone(&self) -> Response

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
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

Creates a default response.

§Examples
use zense::http::Response;

// Create response
let res = Response::default();
Source§

impl Display for Response

Source§

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

Formats the response for display.

Source§

impl IntoResponse for Response

Source§

fn into_response(self) -> Response

Converts the response into itself.

Source§

impl ResponseExt for Response

Source§

fn from_status(status: Status) -> Response

Creates a response from a status code. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.