[−][src]Trait rocket::response::Responder
Trait implemented by types that generate responses for clients.
Types that implement this trait can be used as the return type of a handler, as illustrated below:
#[get("/")] fn index() -> T { ... }
In this example, T can be any type that implements Responder.
Return Value
A Responder returns an Ok(Response) or an Err(Status):
-
An
Okvariant means that theResponderwas successful in generating aResponse. TheResponsewill be written out to the client. -
An
Errvariant means that theRespondercould not or did not generate aResponse. The containedStatuswill be used to find the relevant error catcher which then generates an error response.
Provided Implementations
Rocket implements Responder for several standard library types. Their
behavior is documented here. Note that the Result implementation is
overloaded, allowing for two Responders to be used at once, depending on
the variant.
-
&str
Sets the
Content-Typetotext/plain. The string is used as the body of the response, which is fixed size and not streamed. To stream a raw string, useStream::from(Cursor::new(string)). -
String
Sets the
Content-Typetotext/plain. The string is used as the body of the response, which is fixed size and not streamed. To stream a string, useStream::from(Cursor::new(string)). -
Vec
Sets the
Content-Typetoapplication/octet-stream. The vector's data is used as the body of the response, which is fixed size and not streamed. To stream a vector of bytes, useStream::from(Cursor::new(vec)). -
File
Responds with a streamed body containing the data in the
File. NoContent-Typeis set. To automatically have aContent-Typeset based on the file's extension, useNamedFile. -
()
Responds with an empty body. No
Content-Typeis set. -
Option<T>
If the
OptionisSome, the wrapped responder is used to respond to the client. Otherwise, anErrwith status 404 Not Found is returned and a warning is printed to the console. -
Result<T, E> where E: Debug
If the
ResultisOk, the wrapped responder is used to respond to the client. Otherwise, anErrwith status 500 Internal Server Error is returned and the error is printed to the console using theDebugimplementation. -
Result<T, E> where E: Debug + Responder
If the
ResultisOk, the wrappedOkresponder is used to respond to the client. If theResultisErr, the wrappedErrresponder is used to respond to the client.
Implementation Tips
This section describes a few best practices to take into account when
implementing Responder.
Debug
A type implementing Responder should implement the Debug trait when
possible. This is because the Responder implementation for Result
requires its Err type to implement Debug. Therefore, a type implementing
Debug can more easily be composed.
Joining and Merging
When chaining/wrapping other Responders, use the
merge or
join methods on the Response
or ResponseBuilder struct. Ensure that you document the merging or joining
behavior appropriately.
Inspecting Requests
A Responder has access to the request it is responding to. Even so, you
should avoid using the Request value as much as possible. This is because
using the Request object makes your responder inpure, and so the use of
the type as a Responder has less intrinsic meaning associated with it. If
the Responder were pure, however, it always respond in the same manner,
regardless of the incoming request. Thus, knowing the type is sufficient to
fully determine its functionality.
Example
Say that you have a custom type, Person:
struct Person { name: String, age: u16 }
You'd like to use Person as a Responder so that you can return a
Person directly from a handler:
#[get("/person/<id>")] fn person(id: usize) -> Option<Person> { Person::from_id(id) }
You want the Person responder to set two header fields: X-Person-Name
and X-Person-Age as well as supply a custom representation of the object
(Content-Type: application/x-person) in the body of the response. The
following Responder implementation accomplishes this:
use std::io::Cursor; use rocket::request::Request; use rocket::response::{self, Response, Responder}; use rocket::http::ContentType; impl<'r> Responder<'r> for Person { fn respond_to(self, _: &Request) -> response::Result<'r> { Response::build() .sized_body(Cursor::new(format!("{}:{}", self.name, self.age))) .raw_header("X-Person-Name", self.name) .raw_header("X-Person-Age", self.age.to_string()) .header(ContentType::new("application", "x-person")) .ok() } }
Required Methods
fn respond_to(self, request: &Request) -> Result<Response<'r>, Status>
Returns Ok if a Response could be generated successfully. Otherwise,
returns an Err with a failing Status.
The request parameter is the Request that this Responder is
responding to.
When using Rocket's code generation, if an Ok(Response) is returned,
the response will be written out to the client. If an Err(Status) is
returned, the error catcher for the given status is retrieved and called
to generate a final error response, which is then written out to the
client.
Implementations on Foreign Types
impl<'r> Responder<'r> for &'r str[src]
impl<'r> Responder<'r> for &'r strReturns a response with Content-Type text/plain and a fixed-size body
containing the string self. Always returns Ok.
impl<'r> Responder<'r> for String[src]
impl<'r> Responder<'r> for StringReturns a response with Content-Type text/plain and a fixed-size body
containing the string self. Always returns Ok.
impl<'r> Responder<'r> for Vec<u8>[src]
impl<'r> Responder<'r> for Vec<u8>Returns a response with Content-Type application/octet-stream and a
fixed-size body containing the data in self. Always returns Ok.
fn respond_to(self, _: &Request) -> Result<'r>[src]
fn respond_to(self, _: &Request) -> Result<'r>impl<'r> Responder<'r> for File[src]
impl<'r> Responder<'r> for FileReturns a response with a sized body for the file. Always returns Ok.
fn respond_to(self, _: &Request) -> Result<'r>[src]
fn respond_to(self, _: &Request) -> Result<'r>impl<'r> Responder<'r> for ()[src]
impl<'r> Responder<'r> for ()Returns an empty, default Response. Always returns Ok.
impl<'r, R: Responder<'r>> Responder<'r> for Option<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Option<R>If self is Some, responds with the wrapped Responder. Otherwise prints
a warning message and returns an Err of Status::NotFound.
impl<'r, R: Responder<'r>, E: Debug> Responder<'r> for Result<R, E>[src]
impl<'r, R: Responder<'r>, E: Debug> Responder<'r> for Result<R, E>If self is Ok, responds with the wrapped Responder. Otherwise prints
an error message with the Err value returns an Err of
Status::InternalServerError.
impl<'r, R: Responder<'r>, E: Responder<'r> + Debug> Responder<'r> for Result<R, E>[src]
impl<'r, R: Responder<'r>, E: Responder<'r> + Debug> Responder<'r> for Result<R, E>Responds with the wrapped Responder in self, whether it is Ok or
Err.
Implementors
impl Responder<'static> for Reset[src]
impl Responder<'static> for ResetSets the status code of the response to 205 Reset Content. The body of the response will be empty.
impl Responder<'static> for Failure[src]
impl Responder<'static> for Failureimpl Responder<'static> for Redirect[src]
impl Responder<'static> for RedirectConstructs a response with the appropriate status code and the given URL in
the Location header field. The body of the response is empty. This
responder does not fail.
impl<'r> Responder<'r> for NoContent[src]
impl<'r> Responder<'r> for NoContentSets the status code of the response to 204 No Content. The body of the response will be empty.
impl<'r> Responder<'r> for NamedFile[src]
impl<'r> Responder<'r> for NamedFileStreams the named file to the client. Sets or overrides the Content-Type in
the response according to the file's extension if the extension is
recognized. See
ContentType::from_extension
for more information. If you would like to stream a file with a different
Content-Type than that implied by its extension, use a File directly.
fn respond_to(self, req: &Request) -> Result<'r>[src]
fn respond_to(self, req: &Request) -> Result<'r>impl<'r> Responder<'r> for Response<'r>[src]
impl<'r> Responder<'r> for Response<'r>fn respond_to(self, _: &Request) -> Result<Response<'r>, Status>[src]
fn respond_to(self, _: &Request) -> Result<Response<'r>, Status>This is the identity implementation. It simply returns Ok(self).
impl<'r, R: Responder<'r> + Hash> Responder<'r> for Created<R>[src]
impl<'r, R: Responder<'r> + Hash> Responder<'r> for Created<R>In addition to setting the status code, Location header, and finalizing
the response with the Responder, the ETag header is set conditionally if
a Responder is provided that implements Hash. The ETag header is set
to a hash value of the responder.
impl<'r, R: Responder<'r>> Responder<'r> for Content<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Content<R>Overrides the Content-Type of the response to the wrapped ContentType then
delegates the remainder of the response to the wrapped responder.
impl<'r, R: Responder<'r>> Responder<'r> for Css<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Css<R>Sets the Content-Type of the response then delegates the remainder of the response to the wrapped responder.
impl<'r, R: Responder<'r>> Responder<'r> for Html<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Html<R>Sets the Content-Type of the response then delegates the remainder of the response to the wrapped responder.
impl<'r, R: Responder<'r>> Responder<'r> for JavaScript<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for JavaScript<R>Sets the Content-Type of the response then delegates the remainder of the response to the wrapped responder.
impl<'r, R: Responder<'r>> Responder<'r> for Json<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Json<R>Sets the Content-Type of the response then delegates the remainder of the response to the wrapped responder.
impl<'r, R: Responder<'r>> Responder<'r> for MsgPack<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for MsgPack<R>Sets the Content-Type of the response then delegates the remainder of the response to the wrapped responder.
impl<'r, R: Responder<'r>> Responder<'r> for Plain<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Plain<R>Sets the Content-Type of the response then delegates the remainder of the response to the wrapped responder.
impl<'r, R: Responder<'r>> Responder<'r> for Xml<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Xml<R>Sets the Content-Type of the response then delegates the remainder of the response to the wrapped responder.
impl<'r, R: Responder<'r>> Responder<'r> for Accepted<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Accepted<R>Sets the status code of the response to 202 Accepted. If the responder is
Some, it is used to finalize the response.
impl<'r, R: Responder<'r>> Responder<'r> for BadRequest<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for BadRequest<R>Sets the status code of the response to 400 Bad Request. If the responder is
Some, it is used to finalize the response.
impl<'r, R: Responder<'r>> Responder<'r> for Created<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Created<R>Sets the status code of the response to 201 Created. Sets the Location
header to the String parameter in the constructor.
The optional responder finalizes the response if it exists. The wrapped responder should write the body of the response so that it contains information about the created resource. If no responder is provided, the response body will be empty.
impl<'r, R: Responder<'r>> Responder<'r> for Custom<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Custom<R>Sets the status code of the response and then delegates the remainder of the response to the wrapped responder.
impl<'r, R: Responder<'r>> Responder<'r> for NotFound<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for NotFound<R>Sets the status code of the response to 404 Not Found.
impl<'r, R: Responder<'r>> Responder<'r> for Flash<R>[src]
impl<'r, R: Responder<'r>> Responder<'r> for Flash<R>Sets the message cookie and then uses the wrapped responder to complete the
response. In other words, simply sets a cookie and delagates the rest of the
response handling to the wrapped responder. As a result, the Outcome of
the response is the Outcome of the wrapped Responder.
impl<'r, T: Read + 'r> Responder<'r> for Stream<T>[src]
impl<'r, T: Read + 'r> Responder<'r> for Stream<T>Sends a response to the client using the "Chunked" transfer encoding. The maximum chunk size is 4KiB.
Failure
If reading from the input stream fails at any point during the response, the response is abandoned, and the response ends abruptly. An error is printed to the console with an indication of what went wrong.