pub struct Response { /* private fields */ }
Expand description
A Response representation for
working with or returning a response to a Request
.
Implementations
sourceimpl Response
impl Response
sourcepub fn from_json<B: Serialize>(value: &B) -> Result<Self>
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
.
sourcepub fn from_html(html: impl AsRef<str>) -> Result<Self>
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
.
sourcepub fn from_bytes(bytes: Vec<u8>) -> Result<Self>
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
.
sourcepub fn from_body(body: ResponseBody) -> Result<Self>
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
.
sourcepub fn from_websocket(websocket: WebSocket) -> Result<Self>
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.
sourcepub fn ok(body: impl Into<String>) -> Result<Self>
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
.
sourcepub fn error(msg: impl Into<String>, status: u16) -> Result<Self>
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.
sourcepub fn redirect(url: Url) -> Result<Self>
pub fn redirect(url: Url) -> Result<Self>
Create a Response
which redirects to the specified URL with default status_code of 302
sourcepub fn redirect_with_status(url: Url, status_code: u16) -> Result<Self>
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
sourcepub fn status_code(&self) -> u16
pub fn status_code(&self) -> u16
Get the HTTP Status code of this Response
.
sourcepub async fn json<B: DeserializeOwned>(&mut self) -> Result<B>
pub async fn json<B: DeserializeOwned>(&mut self) -> Result<B>
Access this response’s body encoded as JSON.
sourcepub async fn bytes(&mut self) -> Result<Vec<u8>>
pub async fn bytes(&mut self) -> Result<Vec<u8>>
Access this response’s body encoded as raw bytes.
pub fn websocket(self) -> Option<WebSocket>
sourcepub fn with_headers(self, headers: Headers) -> Self
pub fn with_headers(self, headers: Headers) -> Self
Set this response’s Headers
.
sourcepub fn with_status(self, status_code: u16) -> Self
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.
sourcepub fn with_cors(self, cors: &Cors) -> Result<Self>
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)
}
sourcepub fn with_websocket(self, websocket: Option<WebSocket>) -> Self
pub fn with_websocket(self, websocket: Option<WebSocket>) -> Self
Sets this response’s webSocket
option.
This will require a status code 101 to work.
sourcepub fn headers_mut(&mut self) -> &mut Headers
pub fn headers_mut(&mut self) -> &mut Headers
Get a mutable reference to the Headers
on this response.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Response
impl !Send for Response
impl !Sync for Response
impl Unpin for Response
impl UnwindSafe for Response
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more