Skip to main content

Response

Struct Response 

Source
pub struct Response {
    pub status: StatusCode,
    pub headers: HeaderMap,
    pub body: Body,
}
Expand description

An HTTP response.

Fields§

§status: StatusCode§headers: HeaderMap§body: Body

Implementations§

Source§

impl Response

Source

pub fn new(status: StatusCode, body: Body) -> Response

Source

pub fn status(code: u16) -> Response

Empty response with a status code.

Source

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

Source

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

Source

pub fn xml(body: impl Into<String>) -> Response

XML response (feeds, sitemaps).

Source

pub fn svg(body: impl Into<String>) -> Response

SVG response, for images generated on the fly.

Source

pub fn json<T>(data: &T) -> Response
where T: Serialize + ?Sized,

JSON response. Serialization failures produce a 500.

Source

pub fn bytes(body: impl Into<Bytes>) -> Response

Source

pub fn stream<S, B, E>(stream: S) -> Response
where S: Stream<Item = Result<B, E>> + Send + 'static, B: Into<Bytes>, E: Into<Box<dyn Error + Send + Sync>>,

Stream arbitrary byte chunks.

Source

pub fn redirect(location: &str) -> Response

Temporary redirect (307).

Source

pub fn permanent_redirect(location: &str) -> Response

Permanent redirect (308).

Source

pub fn see_other(location: &str) -> Response

Redirect after a form POST (303 See Other).

Source

pub fn redirect_with(location: &str, status: StatusCode) -> Response

Source

pub fn not_found() -> Response

Source

pub fn with_status(self, code: u16) -> Response

Source

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

Set a header. Invalid names or values (e.g. containing CR/LF) are rejected, which prevents header injection; they are logged and dropped.

Source

pub fn set_header(&mut self, name: &str, value: &str) -> bool

Source

pub fn append_header(&mut self, name: &str, value: &str) -> bool

Source

pub fn with_content_type(self, ct: &'static str) -> Response

Source

pub fn with_cache_control(self, value: &str) -> Response

Add a cookie to the response.

Source

pub fn header(&self, name: &str) -> Option<&str>

Source

pub async fn into_bytes(self) -> Result<Bytes, Box<dyn Error + Send + Sync>>

Collect the body into bytes (tests and adapters).

Source

pub async fn into_text(self) -> String

Collect the body as UTF-8 text (lossy).

Source§

impl Response

Source

pub fn sse<S>(events: S) -> Response
where S: Stream<Item = SseEvent> + Send + 'static,

Stream server-sent events. A keep-alive comment is sent every 15 seconds of inactivity; the response ends when events ends.

Source

pub fn sse_with_keep_alive<S>(events: S, keep_alive: Duration) -> Response
where S: Stream<Item = SseEvent> + Send + 'static,

Trait Implementations§

Source§

impl Debug for Response

Source§

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

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

impl Default for Response

Source§

fn default() -> Response

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

impl IntoResponse for 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<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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

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.