Trait rocket::response::Responder [] [src]

pub trait Responder<'r> {
    fn respond_to(self, request: &Request) -> Result<Response<'r>, Status>;
}

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:

Be careful when using this code, it's not being tested!
#[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):

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.

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:

Be careful when using this code, it's not being tested!
#[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

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]

Returns a response with Content-Type text/plain and a fixed-size body containing the string self. Always returns Ok.

[src]

impl<'r> Responder<'r> for String
[src]

Returns a response with Content-Type text/plain and a fixed-size body containing the string self. Always returns Ok.

[src]

impl<'r> Responder<'r> for Vec<u8>
[src]

Returns a response with Content-Type application/octet-stream and a fixed-size body containing the data in self. Always returns Ok.

[src]

impl<'r> Responder<'r> for File
[src]

Returns a response with a sized body for the file. Always returns Ok.

[src]

impl<'r> Responder<'r> for ()
[src]

Returns an empty, default Response. Always returns Ok.

[src]

impl<'r, R: Responder<'r>> Responder<'r> for Option<R>
[src]

If self is Some, responds with the wrapped Responder. Otherwise prints a warning message and returns an Err of Status::NotFound.

[src]

impl<'r, R: Responder<'r>, E: Debug> Responder<'r> for Result<R, E>
[src]

If self is Ok, responds with the wrapped Responder. Otherwise prints an error message with the Err value returns an Err of Status::InternalServerError.

[src]

impl<'r, R: Responder<'r>, E: Responder<'r> + Debug> Responder<'r> for Result<R, E>
[src]

Responds with the wrapped Responder in self, whether it is Ok or Err.

[src]

Implementors