pub struct Response { /* private fields */ }
Expand description
A Response representation for
working with or returning a response to a Request
.
Implementations§
Source§impl Response
impl Response
Sourcepub fn builder() -> ResponseBuilder
pub fn builder() -> ResponseBuilder
Construct a builder for a new 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; charset=utf-8
.
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 from_stream<S>(stream: S) -> Result<Self>
pub fn from_stream<S>(stream: S) -> Result<Self>
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
.
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; charset=utf-8
.
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 fn body(&self) -> &ResponseBody
pub fn body(&self) -> &ResponseBody
Access this response’s body
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.
Sourcepub fn stream(&mut self) -> Result<ByteStream>
pub fn stream(&mut self) -> Result<ByteStream>
Access this response’s body as a Stream
of 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 encode_body(&self) -> &EncodeBody
pub fn encode_body(&self) -> &EncodeBody
Read the encode_body
configuration for this Response
.
Sourcepub fn with_encode_body(self, encode_body: EncodeBody) -> Self
pub fn with_encode_body(self, encode_body: EncodeBody) -> Self
Set this response’s encodeBody
option.
In most cases this is not needed, but it can be set to “manual” to
return already compressed data to the user without re-compression.
Sourcepub fn cf<T: DeserializeOwned>(&self) -> Result<Option<T>>
pub fn cf<T: DeserializeOwned>(&self) -> Result<Option<T>>
Read the cf
information for this Response
.
Sourcepub fn with_cf<T: Serialize>(self, cf: Option<T>) -> Result<Self>
pub fn with_cf<T: Serialize>(self, cf: Option<T>) -> Result<Self>
Set this response’s cf
options. This is used by consumers of the Response
for
informational purposes and has no impact on Workers behavior.
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.
Sourcepub fn into_parts(self) -> (ResponseBuilder, ResponseBody)
pub fn into_parts(self) -> (ResponseBuilder, ResponseBody)
Split the response into ResponseBuilder
and ResponseBody
so that it
can be modified.