pub trait RequestExt {
// Required methods
fn params(&self) -> &RouteParams;
fn param<P: Into<String>>(&self, param_name: P) -> Option<&String>;
fn remote_addr(&self) -> SocketAddr;
fn data<T: Send + Sync + 'static>(&self) -> Option<&T>;
fn context<T: Send + Sync + Clone + 'static>(&self) -> Option<T>;
fn set_context<T: Send + Sync + Clone + 'static>(&self, val: T);
}Expand description
A extension trait which extends the hyper::Request and http::Parts types with some helpful methods.
Required Methods§
Sourcefn params(&self) -> &RouteParams
fn params(&self) -> &RouteParams
It returns the route parameters as RouteParams type with the name of the parameter specified in the path as their respective keys.
§Examples
use http_body_util::Full;
use hyper::{body::Bytes, Response};
use routerify_ng::ext::RequestExt;
use routerify_ng::{RouteParams, Router};
use std::convert::Infallible;
use hyper::body::Incoming;
fn run() -> Router<hyper::Error> {
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(Full::new(Bytes::from(format!(
"Username: {}, Book Name: {}",
user_name, book_name
)))))
})
.build()
.unwrap();
router
}Sourcefn param<P: Into<String>>(&self, param_name: P) -> Option<&String>
fn param<P: Into<String>>(&self, param_name: P) -> Option<&String>
It returns the route parameter value by the name of the parameter specified in the path.
§Examples
use http_body_util::Full;
use hyper::body::Bytes;
use hyper::Response;
use routerify_ng::ext::RequestExt;
use routerify_ng::Router;
use std::convert::Infallible;
use hyper::body::Incoming;
fn run() -> Router<Infallible> {
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(Full::new(Bytes::from(format!(
"Username: {}, Book Name: {}",
user_name, book_name
)))))
})
.build()
.unwrap();
router
}Sourcefn remote_addr(&self) -> SocketAddr
fn remote_addr(&self) -> SocketAddr
It returns the remote address of the incoming request.
§Examples
use http_body_util::Full;
use hyper::{body::Bytes, Response};
use routerify_ng::ext::RequestExt;
use routerify_ng::Router;
use std::convert::Infallible;
use hyper::body::Incoming;
fn run() -> Router<Infallible> {
let router = Router::builder()
.get("/hello", |req| async move {
let remote_addr = req.remote_addr();
Ok(Response::new(Full::new(Bytes::from(format!(
"Hello from : {}",
remote_addr
)))))
})
.build()
.unwrap();
router
}Sourcefn data<T: Send + Sync + 'static>(&self) -> Option<&T>
fn data<T: Send + Sync + 'static>(&self) -> Option<&T>
Access data which was shared by the RouterBuilder method
data.
Please refer to the Data and State Sharing for more info.
Sourcefn context<T: Send + Sync + Clone + 'static>(&self) -> Option<T>
fn context<T: Send + Sync + Clone + 'static>(&self) -> Option<T>
Access data in the request context.
Sourcefn set_context<T: Send + Sync + Clone + 'static>(&self, val: T)
fn set_context<T: Send + Sync + Clone + 'static>(&self, val: T)
Put data into the request context.
§Examples
use http_body_util::Full;
use hyper::{body::Bytes, Request, Response};
use routerify_ng::ext::RequestExt;
use routerify_ng::{Middleware, Router};
use std::convert::Infallible;
fn run() -> Router<Infallible> {
let router = Router::builder()
.middleware(Middleware::pre(|req: Request<Full<Bytes>>| async move {
req.set_context("example".to_string());
Ok(req)
}))
.get("/hello", |req| async move {
let text = req.context::<String>().unwrap();
Ok(Response::new(Full::new(Bytes::from(format!("Hello from : {}", text)))))
})
.build()
.unwrap();
router
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.