pub struct Request<T = Body<Bytes>> { /* private fields */ }
Expand description
Struct that wraps a hyper request + some magic
Implementations§
source§impl Request<Body<Bytes>>
impl Request<Body<Bytes>>
pub async fn json<T>(&mut self) -> Result<T, SaphirError>where T: for<'a> Deserialize<'a> + Unpin + 'static,
json
only.source§impl Request<Body<Bytes>>
impl Request<Body<Bytes>>
pub async fn form<T>(&mut self) -> Result<T, SaphirError>where T: for<'a> Deserialize<'a> + Unpin + 'static,
form
only.source§impl<T> Request<T>
impl<T> Request<T>
sourcepub fn peer_addr(&self) -> Option<&SocketAddr>
pub fn peer_addr(&self) -> Option<&SocketAddr>
Return the Peer SocketAddr if one was available when receiving the request
sourcepub fn operation_id(&self) -> &OperationId
Available on crate feature operation
only.
pub fn operation_id(&self) -> &OperationId
operation
only.Return the OperationId of the request
sourcepub fn operation_id_mut(&mut self) -> &mut OperationId
Available on crate feature operation
only.
pub fn operation_id_mut(&mut self) -> &mut OperationId
operation
only.Return the mutable OperationId of the request
sourcepub fn peer_addr_mut(&mut self) -> Option<&mut SocketAddr>
pub fn peer_addr_mut(&mut self) -> Option<&mut SocketAddr>
Get the cookies sent by the browsers.
Before accessing cookies, you will need to parse them, it is done with
the parse_cookies
method
// Parse cookies
req.parse_cookies();
// then use cookies
let cookie = req.cookies().get("MyCookie");
Get the cookies sent by the browsers in a mutable way
Before accessing cookies, you will need to parse them, it is done with
the parse_cookies
method
// Parse cookies
req.parse_cookies();
// then use cookies
let mut_cookie = req.cookies_mut().get("MyCookie");
sourcepub fn captures(&self) -> &HashMap<String, String>
pub fn captures(&self) -> &HashMap<String, String>
Access the captured variables from the request path. E.g. a path
composed as /user/{user_id}/profile
will store a capture named
"user_id"
.
let user_id = req.captures().get("user_id");
// retrieve user by id
sourcepub fn captures_mut(&mut self) -> &mut HashMap<String, String>
pub fn captures_mut(&mut self) -> &mut HashMap<String, String>
Access the captured variables from the request path, in a mutable way.
sourcepub fn map<F, U>(self, f: F) -> Request<U>where
F: FnOnce(T) -> U,
pub fn map<F, U>(self, f: F) -> Request<U>where F: FnOnce(T) -> U,
Convert a request of T in a request of U
// req is Request<Body>
let req: Request<String> = req.map(|_ignored_body| "New body".to_string());
sourcepub async fn async_map<F, Fut, U>(self, f: F) -> Request<U>where
F: FnOnce(T) -> Fut,
Fut: Future<Output = U>,
pub async fn async_map<F, Fut, U>(self, f: F) -> Request<U>where F: FnOnce(T) -> Fut, Fut: Future<Output = U>,
Convert a request of T in a request of U through a future
// req is Request<Body>
let req = req.async_map(|b| async {hyper::body::to_bytes(b).await});
sourcepub fn into_body(self) -> T
pub fn into_body(self) -> T
Return body, dropping the request
// req is Request<Body<Bytes>>
let body = req.into_body();
Parse cookies from the Cookie header
Methods from Deref<Target = RawRequest<T>>§
sourcepub fn method(&self) -> &Method
pub fn method(&self) -> &Method
Returns a reference to the associated HTTP method.
Examples
let request: Request<()> = Request::default();
assert_eq!(*request.method(), Method::GET);
sourcepub fn method_mut(&mut self) -> &mut Method
pub fn method_mut(&mut self) -> &mut Method
Returns a mutable reference to the associated HTTP method.
Examples
let mut request: Request<()> = Request::default();
*request.method_mut() = Method::PUT;
assert_eq!(*request.method(), Method::PUT);
sourcepub fn uri(&self) -> &Uri
pub fn uri(&self) -> &Uri
Returns a reference to the associated URI.
Examples
let request: Request<()> = Request::default();
assert_eq!(*request.uri(), *"/");
sourcepub fn uri_mut(&mut self) -> &mut Uri
pub fn uri_mut(&mut self) -> &mut Uri
Returns a mutable reference to the associated URI.
Examples
let mut request: Request<()> = Request::default();
*request.uri_mut() = "/hello".parse().unwrap();
assert_eq!(*request.uri(), *"/hello");
sourcepub fn version(&self) -> Version
pub fn version(&self) -> Version
Returns the associated version.
Examples
let request: Request<()> = Request::default();
assert_eq!(request.version(), Version::HTTP_11);
sourcepub fn version_mut(&mut self) -> &mut Version
pub fn version_mut(&mut self) -> &mut Version
Returns a mutable reference to the associated version.
Examples
let mut request: Request<()> = Request::default();
*request.version_mut() = Version::HTTP_2;
assert_eq!(request.version(), Version::HTTP_2);
sourcepub fn headers(&self) -> &HeaderMap<HeaderValue>
pub fn headers(&self) -> &HeaderMap<HeaderValue>
Returns a reference to the associated header field map.
Examples
let request: Request<()> = Request::default();
assert!(request.headers().is_empty());
sourcepub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
Returns a mutable reference to the associated header field map.
Examples
let mut request: Request<()> = Request::default();
request.headers_mut().insert(HOST, HeaderValue::from_static("world"));
assert!(!request.headers().is_empty());
sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Returns a reference to the associated extensions.
Examples
let request: Request<()> = Request::default();
assert!(request.extensions().get::<i32>().is_none());
sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Returns a mutable reference to the associated extensions.
Examples
let mut request: Request<()> = Request::default();
request.extensions_mut().insert("hello");
assert_eq!(request.extensions().get(), Some(&"hello"));