pub struct Response { /* private fields */ }
Expand description
Each request ultimately ends in a Response
that is served to the
client and then discarded, like fallen flower petals. Together
with Request
and
Responder
it forms the holy trinity of
R
’s in Vial.
Rather than use the “Builder” pattern like more mature and better
designed libraries, Vial’s Response
lets you set properties
either directly or using Builder-style methods:
vial::routes! {
GET "/404" => |_| Response::from(404)
.with_header("Content-Type", "text/plain")
.with_body("404 Not Found");
}
It also defaults to text/html
, so you need to use
with_header()
or
header()
to send plain text.
Implementations§
Source§impl Response
impl Response
Sourcepub fn content_type(&self) -> &str
pub fn content_type(&self) -> &str
Either the inferred or user-set Content-Type for this Response.
Sourcepub fn headers(&self) -> &HashMap<String, String>
pub fn headers(&self) -> &HashMap<String, String>
Take a peek at all the headers for this response.
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
Get an individual header. name
is case insensitive.
Sourcepub fn set_header(&mut self, name: &str, value: &str)
pub fn set_header(&mut self, name: &str, value: &str)
Set an individual header.
Sourcepub fn from_asset(path: &str) -> Response
pub fn from_asset(path: &str) -> Response
Create a response from an asset. See the
asset
module for more information on
using assets.
Sourcepub fn from_reader(reader: Box<dyn Read>) -> Response
pub fn from_reader(reader: Box<dyn Read>) -> Response
Create a response from a (boxed) io::Read.
Sourcepub fn from_error<E: Error>(err: E) -> Response
pub fn from_error<E: Error>(err: E) -> Response
Creates a 500 response from an error, displaying it.
Sourcepub fn from_header(name: &str, value: &str) -> Response
pub fn from_header(name: &str, value: &str) -> Response
Creates a new Response and sets the given header, in addition to the defaults.
Sourcepub fn from_body<S: AsRef<str>>(body: S) -> Response
pub fn from_body<S: AsRef<str>>(body: S) -> Response
Creates a new default Response with the given body.
Sourcepub fn from_text<S: AsRef<str>>(text: S) -> Response
pub fn from_text<S: AsRef<str>>(text: S) -> Response
Creates a new text/plain
Response with the given body.
Sourcepub fn from_code(code: usize) -> Response
pub fn from_code(code: usize) -> Response
Creates a new response with the given HTTP Status Code.
Sourcepub fn with_code(self, code: usize) -> Response
pub fn with_code(self, code: usize) -> Response
Creates a new response with the given HTTP Status Code.
Sourcepub fn with_body<S: AsRef<str>>(self, body: S) -> Response
pub fn with_body<S: AsRef<str>>(self, body: S) -> Response
Body builder. Returns a Response with the given body.
Sourcepub fn with_text<S: AsRef<str>>(self, text: S) -> Response
pub fn with_text<S: AsRef<str>>(self, text: S) -> Response
Returns a text/plain
Response with the given body.
Sourcepub fn with_reader(self, reader: Box<dyn Read>) -> Response
pub fn with_reader(self, reader: Box<dyn Read>) -> Response
Returns a Response using the given reader for the body.
Sourcepub fn with_asset(self, path: &str) -> Response
pub fn with_asset(self, path: &str) -> Response
Uses an asset for the given body and sets the Content-Type
header based on the file’s extension.
See the asset
module for more
information on using assets.
Sourcepub fn with_file(self, path: &str) -> Response
pub fn with_file(self, path: &str) -> Response
Sets this Response’s body to the body of the given file and
sets the Content-Type
header based on the file’s extension.
Sourcepub fn with_error<E: Error>(self, err: E) -> Response
pub fn with_error<E: Error>(self, err: E) -> Response
Sets the response code to 500 and the body to the error’s text.
Sourcepub fn with_header(self, key: &str, value: &str) -> Response
pub fn with_header(self, key: &str, value: &str) -> Response
Returns a Response with the given header set to the value.
Sourcepub fn redirect_to<U: AsRef<str>>(url: U) -> Response
pub fn redirect_to<U: AsRef<str>>(url: U) -> Response
Returns a 302 redirect to the given URL.