Struct rouille::Response
[−]
[src]
pub struct Response {
pub status_code: u16,
pub headers: Vec<(String, String)>,
pub data: ResponseBody,
}Contains a prototype of a response.
The response is only sent to the client when you return the Response object from your
request handler. This means that you are free to create as many Response objects as you want.
Fields
status_code: u16
The status code to return to the user.
headers: Vec<(String, String)>
List of headers to be returned in the response.
Note that important headers such as Connection or Content-Length will be ignored
from this list.
data: ResponseBody
An opaque type that contains the body of the response.
Methods
impl Response[src]
fn success(&self) -> bool
Returns true if the status code of this Response indicates success.
This is the range [200-399].
Example
use rouille::Response; let response = Response::text("hello world"); assert!(response.success());
fn error(&self) -> bool
Shortcut for !response.success().
Example
use rouille::Response; let response = Response::empty_400(); assert!(response.error());
fn redirect(target: &str) -> Response
Builds a Response that redirects the user to another URL with a 303 status code.
Example
use rouille::Response; let response = Response::redirect("/foo");
fn html<D>(content: D) -> Response where D: Into<Vec<u8>>
Builds a Response that outputs HTML.
Example
use rouille::Response; let response = Response::text("<p>hello <strong>world</strong></p>");
fn svg<D>(content: D) -> Response where D: Into<Vec<u8>>
Builds a Response that outputs SVG.
Example
use rouille::Response; let response = Response::svg("<svg xmlns='http://www.w3.org/2000/svg'/>");
fn text<S>(text: S) -> Response where S: Into<String>
Builds a Response that outputs plain text.
Example
use rouille::Response; let response = Response::text("hello world");
fn json<T>(content: &T) -> Response where T: Encodable
Builds a Response that outputs JSON.
Example
extern crate rustc_serialize; use rouille::Response; #[derive(RustcEncodable)] struct MyStruct { field1: String, field2: i32, } let response = Response::json(&MyStruct { field1: "hello".to_owned(), field2: 5 }); // The Response will contain something like `{ field1: "hello", field2: 5 }`
fn basic_http_auth_login_required(realm: &str) -> Response
Builds a Response that returns a 401 Not Authorized status
and a WWW-Authenticate header.
Example
use rouille::Response; let response = Response::basic_http_auth_login_required("realm");
fn empty_400() -> Response
Builds an empty Response with a 400 status code.
Example
use rouille::Response; let response = Response::empty_400();
fn empty_404() -> Response
Builds an empty Response with a 404 status code.
Example
use rouille::Response; let response = Response::empty_404();
fn with_status_code(self, code: u16) -> Response
Changes the status code of the response.
Example
use rouille::Response; let response = Response::text("hello world").with_status_code(500);