worker_route::http

Struct ResponseBuilder

Source
pub struct ResponseBuilder { /* private fields */ }
Expand description

An alternative worker::Response builder.

§Examples

use worker::{Request, RouteContext};
use worker_route::{get, http::ContentType, HttpResponse, http::ResponseBuilder};

#[get("/hello_world")]
fn hello_world(_: Request, _: RouteContext<()>) -> worker::Result<HttpResponse> {
    Ok(ResponseBuilder::init()
        .content_type(&ContentType::plaintext())
        .body(String::from("Hello world.")))
}

Implementations§

Source§

impl ResponseBuilder

Source

pub fn init() -> Self

Constructs a response builder.

Source

pub fn new(status: StatusCode) -> Self

Constructs a response builder with HTTP status.

Source

pub fn content_type(self, v: &ContentType) -> Self

Set response content type.

Source

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

Set response status code.

Source

pub fn set_status(&mut self, status: StatusCode) -> &mut Self

Set response status code.

Source

pub fn body<B: Into<Body>>(self, body: B) -> HttpResponse

Set a body to the response.

Source

pub fn bytes(self, bytes: Vec<u8>) -> HttpResponse

Set the bytes to the response body and content-type as application/octet-stream.

Source

pub fn html(self, html: &str) -> HttpResponse

Set the HTML content to the response body and content-type as text/html.

Source

pub fn json<S: Serialize>(self, value: S) -> HttpResponse

Set the JSON to the response body and content-type as application/json.

Source

pub fn text<T: Into<String>>(self, body: T) -> HttpResponse

Set a body to the response.

Source

pub fn insert_header(&mut self, k: HeaderName, v: HeaderValue) -> &mut Self

Insert a header, replacing any that were set with an equivalent field name.

§Examples
use worker::{Request, RouteContext};
use worker_route::{get, http};
use http::{
    header::{HeaderName, HeaderValue},
    HttpResponse, ResponseBuilder, ContentType,
};

#[get("/hello_world")]
fn hello_world(_: Request, _: RouteContext<()>) -> worker::Result<HttpResponse> {
    let mut res = ResponseBuilder::init();
    res.insert_header(
        HeaderName::from_static("my-header"),
        HeaderValue::from_static("my-value"),
    );

    Ok(res.body(String::from("Hello world.")))
}
§Errors

Errors are returned to the response if the header name or value is invalid or contains empty spaces.

§Panics

Panics if HeaderName or HeaderValue is constructed from using the method from_static and the static string is an invalid header or contains spaces.

Source

pub fn append_header(&mut self, k: HeaderName, v: HeaderValue) -> &mut Self

Append a header, keeping any that were set with an equivalent field name.

§Examples
use http::{
    header::{HeaderName, HeaderValue},
    HttpResponse, ResponseBuilder, ContentType,
};
use worker::{Request, RouteContext};
use worker_route::{get, http};

#[get("/hello_world")]
fn hello_world(_: Request, _: RouteContext<()>) -> worker::Result<HttpResponse> {
    let mut res = ResponseBuilder::init();
    res.append_header(
        HeaderName::from_static("my-header"),
        HeaderValue::from_static("my-value"),
    );

    Ok(res.body(String::from("Hello world.")))
}
§Errors

Errors are returned to the response if the header name or value is invalid or contains empty spaces.

§Panics

Panics if HeaderName or HeaderValue is constructed from using the method from_static and the static string is an invalid header or contains empty spaces.

Available on crate feature cookies only.

Add a cookie to this response.

Source

pub fn cookies(&self) -> impl Iterator<Item = Cookie<'_>>

Available on crate feature cookies only.

Get an iterator for the cookies set by this response.

Source

pub fn set_content_type(&mut self, v: &ContentType) -> &mut Self

Set response content type.

Source

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

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

§Examples
use worker::{Request, Method, RouteContext, Cors};
use worker_route::{get, http::ContentType, HttpResponse, http::ResponseBuilder};

#[get("/hello_world")]
fn hello_world(_: Request, _: RouteContext<()>) -> worker::Result<HttpResponse> {
    let mut res = ResponseBuilder::init();
    let my_cors = Cors::default()
       .with_origins(&["*".to_string()])
       .with_allowed_headers(&["method".to_string(), "origin".to_string()])
       .with_methods([Method::Get, Method::Options])
       .with_max_age(86400);

    res.with_cors(&my_cors);

    Ok(res
        .content_type(&ContentType::plaintext())
        .body(String::from("Hello world.")))
}
Source

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

Available on crate feature experimental only.
Source

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

Available on crate feature experimental only.
Source

pub fn headers(&self) -> &HttpHeaders

Read the HttpHeaders on this response.

Source

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

Get a mutable reference to the Headers on this response.

Source

pub fn status_code(&self) -> Option<&StatusCode>

Read the StatusCode on this response.

Source

pub fn status_mut(&mut self) -> Option<&mut StatusCode>

Set the StatusCode on this response.

Trait Implementations§

Source§

impl Debug for ResponseBuilder

Source§

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

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

impl Default for ResponseBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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, 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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T