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
method: Method
path: String
payload: Vec<u8>
params: HashMap<String, String>
Methods
impl Request
[src]
fn new() -> Request
Create a new, empty Request.
fn from_str(rqstr: &str) -> Request
Create a Request from an HTTP request string.
fn get_header(&self, name: &str) -> Option<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), } }
fn get<T: FromUri>(&self, name: &str) -> T
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) }
fn get_json(&self) -> Result<Value, RequestError>
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), } }
fn get_json_obj<T>(&self) -> Result<T, RequestError> where
T: DeserializeOwned,
T: DeserializeOwned,
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), } }