[][src]Trait routerify::ext::RequestExt

pub trait RequestExt {
    pub fn params(&self) -> &RouteParams;
pub fn param<P: Into<String>>(&self, param_name: P) -> Option<&String>;
pub fn remote_addr(&self) -> SocketAddr;
pub fn data<T: Send + Sync + 'static>(&self) -> Option<&T>; }

A extension trait which extends the hyper::Request type with some helpful methods.

Required methods

pub fn params(&self) -> &RouteParams[src]

It returns the route parameters as RouteParams type with the name of the parameter specified in the path as their respective keys.

Examples

use routerify::{Router, RouteParams};
use routerify::ext::RequestExt;
use hyper::{Response, Body};

let router = Router::builder()
    .get("/users/:userName/books/:bookName", |req| async move {
        let params: &RouteParams = req.params();
        let user_name = params.get("userName").unwrap();
        let book_name = params.get("bookName").unwrap();

        Ok(Response::new(Body::from(format!("Username: {}, Book Name: {}", user_name, book_name))))
     })
     .build()
     .unwrap();

pub fn param<P: Into<String>>(&self, param_name: P) -> Option<&String>[src]

It returns the route parameter value by the name of the parameter specified in the path.

Examples

use routerify::{Router, RouteParams};
use routerify::ext::RequestExt;
use hyper::{Response, Body};

let router = Router::builder()
    .get("/users/:userName/books/:bookName", |req| async move {
        let user_name = req.param("userName").unwrap();
        let book_name = req.param("bookName").unwrap();

        Ok(Response::new(Body::from(format!("Username: {}, Book Name: {}", user_name, book_name))))
     })
     .build()
     .unwrap();

pub fn remote_addr(&self) -> SocketAddr[src]

It returns the remote address of the incoming request.

Examples

use routerify::{Router, RouteParams};
use routerify::ext::RequestExt;
use hyper::{Response, Body};

let router = Router::builder()
    .get("/hello", |req| async move {
        let remote_addr = req.remote_addr();

        Ok(Response::new(Body::from(format!("Hello from : {}", remote_addr))))
     })
     .build()
     .unwrap();

pub fn data<T: Send + Sync + 'static>(&self) -> Option<&T>[src]

Access data which was shared by the RouterBuilder method data.

Please refer to the Data and State Sharing for more info.

Loading content...

Implementations on Foreign Types

impl RequestExt for Request<Body>[src]

Loading content...

Implementors

Loading content...