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
impl ResponseBuilder
Sourcepub fn new(status: StatusCode) -> Self
pub fn new(status: StatusCode) -> Self
Constructs a response builder with HTTP status.
Sourcepub fn content_type(self, v: &ContentType) -> Self
pub fn content_type(self, v: &ContentType) -> Self
Set response content type.
Sourcepub fn status(self, status: StatusCode) -> Self
pub fn status(self, status: StatusCode) -> Self
Set response status code.
Sourcepub fn set_status(&mut self, status: StatusCode) -> &mut Self
pub fn set_status(&mut self, status: StatusCode) -> &mut Self
Set response status code.
Sourcepub fn body<B: Into<Body>>(self, body: B) -> HttpResponse
pub fn body<B: Into<Body>>(self, body: B) -> HttpResponse
Set a body to the response.
Sourcepub fn bytes(self, bytes: Vec<u8>) -> HttpResponse
pub fn bytes(self, bytes: Vec<u8>) -> HttpResponse
Set the bytes to the response body and content-type as application/octet-stream.
Sourcepub fn html(self, html: &str) -> HttpResponse
pub fn html(self, html: &str) -> HttpResponse
Set the HTML content to the response body and content-type as text/html.
Sourcepub fn json<S: Serialize>(self, value: S) -> HttpResponse
pub fn json<S: Serialize>(self, value: S) -> HttpResponse
Set the JSON to the response body and content-type as application/json.
Sourcepub fn text<T: Into<String>>(self, body: T) -> HttpResponse
pub fn text<T: Into<String>>(self, body: T) -> HttpResponse
Set a body to the response.
Sourcepub fn insert_header(&mut self, k: HeaderName, v: HeaderValue) -> &mut Self
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.
Sourcepub fn append_header(&mut self, k: HeaderName, v: HeaderValue) -> &mut Self
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.
cookies only.Add a cookie to this response.
Available on crate feature cookies only.
cookies only.Get an iterator for the cookies set by this response.
Sourcepub fn set_content_type(&mut self, v: &ContentType) -> &mut Self
pub fn set_content_type(&mut self, v: &ContentType) -> &mut Self
Set response content type.
Sourcepub fn with_cors(&mut self, cors: &Cors) -> &mut Self
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.")))
}pub fn websocket(self, websocket: WebSocket) -> Self
experimental only.pub fn stream<S>(self, stream: S) -> Self
experimental only.Sourcepub fn headers(&self) -> &HttpHeaders
pub fn headers(&self) -> &HttpHeaders
Read the HttpHeaders on this response.
Sourcepub fn headers_mut(&mut self) -> &mut HttpHeaders
pub fn headers_mut(&mut self) -> &mut HttpHeaders
Get a mutable reference to the Headers on this response.
Sourcepub fn status_code(&self) -> Option<&StatusCode>
pub fn status_code(&self) -> Option<&StatusCode>
Read the StatusCode on this response.
Sourcepub fn status_mut(&mut self) -> Option<&mut StatusCode>
pub fn status_mut(&mut self) -> Option<&mut StatusCode>
Set the StatusCode on this response.