Struct canteen::request::Request [] [src]

pub struct Request {
    pub method: Method,
    pub path: String,
    pub payload: Vec<u8>,
    pub params: HashMap<String, String>,
    // some fields omitted
}

This struct represents a request from an HTTP client.

Fields

Methods

impl Request
[src]

Create a new, empty Request.

Create a Request from an HTTP request string.

Get an HTTP header contained in the Request.

Examples

use canteen::{Request, Response};
use canteen::utils;

// Given the route "/hello"
fn handler(req: &Request) -> Response {
    let browser = req.get_header("User-Agent");

    match browser {
        Some(ua) => utils::make_response(format!("You're using {}!", ua), "text/plain", 200),
        None     => utils::make_response("Bad browser, no user agent!", "text/plain", 200),
    }
}

Get a variable from the URI.

Examples

use canteen::{Request, Response};
use canteen::utils;

// Given the route "/hello/<str:name>"
fn handler(req: &Request) -> Response {
    let name: String = req.get("name");
    utils::make_response(format!("<b>Hello, {}!</b>", name), "text/html", 200)
}

Get a raw JSON payload from the request.

Examples

use canteen::{Request, Response};
use canteen::utils;

// Given the POST route "/hello"
fn handler(req: &Request) -> Response {
    let data = req.get_json();

    match data {
        Ok(val) => utils::make_response(format!("We got: {}", val), "text/plain", 200),
        Err(_)  => utils::make_response("We got nothing :(", "text/plain", 200),
    }
}

Get a composed JSON payload from the request.

Examples

use canteen::{Request, Response};

#[derive(RustcDecodable)]
struct Foo {
    item: i32,
}

// Given the POST route "/hello"
fn handler(req: &Request) -> Response {
    let data: Foo = req.get_json_obj();

    match data {
        Ok(foo) => utils::make_response(format!("We got: {}!", data.item), "text/plain", 200),
        Err(_)  => utils::make_response("We got nothing :(", "text/plain", 200),
    }
}

Trait Implementations

impl Debug for Request
[src]

Formats the value using the given formatter.