Struct worker::Response

source ·
pub struct Response { /* private fields */ }
Expand description

A Response representation for working with or returning a response to a Request.

Implementations§

source§

impl Response

source

pub fn from_json<B: Serialize>(value: &B) -> Result<Self>

Create a Response using B as the body encoded as JSON. Sets the associated Content-Type header for the Response as application/json.

source

pub fn from_html(html: impl AsRef<str>) -> Result<Self>

Create a Response using the body encoded as HTML. Sets the associated Content-Type header for the Response as text/html; charset=utf-8.

source

pub fn from_bytes(bytes: Vec<u8>) -> Result<Self>

Create a Response using unprocessed bytes provided. Sets the associated Content-Type header for the Response as application/octet-stream.

source

pub fn from_body(body: ResponseBody) -> Result<Self>

Create a Response using a ResponseBody variant. Sets a status code of 200 and an empty set of Headers. Modify the Response with methods such as with_status and with_headers.

source

pub fn from_websocket(websocket: WebSocket) -> Result<Self>

Create a Response using a WebSocket client. Configures the browser to switch protocols (using status code 101) and returns the websocket.

source

pub fn from_stream<S>(stream: S) -> Result<Self>
where S: TryStream + 'static, S::Ok: Into<Vec<u8>>, S::Error: Into<Error>,

Create a Response using a Stream for the body. Sets a status code of 200 and an empty set of Headers. Modify the Response with methods such as with_status and with_headers.

source

pub fn ok(body: impl Into<String>) -> Result<Self>

Create a Response using unprocessed text provided. Sets the associated Content-Type header for the Response as text/plain; charset=utf-8.

source

pub fn empty() -> Result<Self>

Create an empty Response with a 200 status code.

source

pub fn error(msg: impl Into<String>, status: u16) -> Result<Self>

A helper method to send an error message to a client. Will return Err if the status code provided is outside the valid HTTP error range of 400-599.

source

pub fn redirect(url: Url) -> Result<Self>

Create a Response which redirects to the specified URL with default status_code of 302

source

pub fn redirect_with_status(url: Url, status_code: u16) -> Result<Self>

Create a Response which redirects to the specified URL with a custom status_code

source

pub fn status_code(&self) -> u16

Get the HTTP Status code of this Response.

source

pub fn body(&self) -> &ResponseBody

Access this response’s body

source

pub async fn text(&mut self) -> Result<String>

Access this response’s body as plaintext.

source

pub async fn json<B: DeserializeOwned>(&mut self) -> Result<B>

Access this response’s body encoded as JSON.

source

pub async fn bytes(&mut self) -> Result<Vec<u8>>

Access this response’s body encoded as raw bytes.

source

pub fn stream(&mut self) -> Result<ByteStream>

Access this response’s body as a Stream of bytes.

source

pub fn websocket(self) -> Option<WebSocket>

source

pub fn with_headers(self, headers: Headers) -> Self

Set this response’s Headers.

source

pub fn with_status(self, status_code: u16) -> Self

Set this response’s status code. The Workers platform will reject HTTP status codes outside the range of 200..599 inclusive, and will throw a JavaScript RangeError, returning a response with an HTTP 500 status code.

source

pub fn with_cors(self, cors: &Cors) -> Result<Self>

Sets this response’s cors headers from the Cors struct. Example usage:

use worker::*;
fn fetch() -> worker::Result<Response> {
    let cors = Cors::default();
    Response::empty()?
        .with_cors(&cors)
}
source

pub fn with_websocket(self, websocket: Option<WebSocket>) -> Self

Sets this response’s webSocket option. This will require a status code 101 to work.

source

pub fn headers(&self) -> &Headers

Read the Headers on this response.

source

pub fn headers_mut(&mut self) -> &mut Headers

Get a mutable reference to the Headers on this response.

source

pub fn cloned(&mut self) -> Result<Self>

Clones the response so it can be used multiple times.

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 Response

source§

fn from(res: &Response) -> 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<Response> for Response<Body>

source§

fn from(resp: Response) -> Response<Body>

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<B: Body<Data = Bytes> + 'static> TryFrom<Response<B>> for Response

§

type Error = Error

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

fn try_from(res: Response<B>) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<Response> for HttpResponse

§

type Error = Error

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

fn try_from(res: Response) -> Result<HttpResponse>

Performs the conversion.

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

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.
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