Struct salvo_core::http::request::Request
source · pub struct Request { /* private fields */ }
Expand description
Represents an HTTP request.
Stores all the properties of the client’s request.
Implementations§
source§impl Request
impl Request
sourcepub fn from_hyper<B>(req: Request<B>, scheme: Scheme) -> Self
pub fn from_hyper<B>(req: Request<B>, scheme: Scheme) -> Self
Creates a new Request
from hyper::Request
.
sourcepub fn uri(&self) -> &Uri
pub fn uri(&self) -> &Uri
Returns a reference to the associated URI.
§Examples
let req = Request::default();
assert_eq!(*req.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.
Notice: If you using this mutable reference to change the uri, you should change the params
and queries
manually.
§Examples
let mut req: Request= Request::default();
*req.uri_mut() = "/hello".parse().unwrap();
assert_eq!(*req.uri(), *"/hello");
sourcepub fn set_uri(&mut self, uri: Uri)
pub fn set_uri(&mut self, uri: Uri)
Set the associated URI. querie
will be reset.
Notice: params
will not reset.
sourcepub fn method(&self) -> &Method
pub fn method(&self) -> &Method
Returns a reference to the associated HTTP method.
§Examples
let req = Request::default();
assert_eq!(*req.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 version_mut(&mut self) -> &mut Version
pub fn version_mut(&mut self) -> &mut Version
Returns a mutable reference to the associated version.
sourcepub fn scheme_mut(&mut self) -> &mut Scheme
pub fn scheme_mut(&mut self) -> &mut Scheme
Returns a mutable reference to the associated scheme.
sourcepub fn remote_addr(&self) -> &SocketAddr
pub fn remote_addr(&self) -> &SocketAddr
Get request remote address.
sourcepub fn remote_addr_mut(&mut self) -> &mut SocketAddr
pub fn remote_addr_mut(&mut self) -> &mut SocketAddr
Get request remote address.
sourcepub fn local_addr(&self) -> &SocketAddr
pub fn local_addr(&self) -> &SocketAddr
Get request remote address reference.
sourcepub fn local_addr_mut(&mut self) -> &mut SocketAddr
pub fn local_addr_mut(&mut self) -> &mut SocketAddr
Get mutable request remote address reference.
sourcepub fn headers(&self) -> &HeaderMap
pub fn headers(&self) -> &HeaderMap
Returns a reference to the associated header field map.
§Examples
let req = Request::default();
assert!(req.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 req: Request = Request::default();
req.headers_mut().insert(HOST, HeaderValue::from_static("world"));
assert!(!req.headers().is_empty());
sourcepub fn header<'de, T>(&'de self, key: impl AsHeaderName) -> Option<T>where
T: Deserialize<'de>,
pub fn header<'de, T>(&'de self, key: impl AsHeaderName) -> Option<T>where
T: Deserialize<'de>,
Get header with supplied name and try to parse to a ‘T’, returns None if failed or not found.
sourcepub fn add_header<N, V>(
&mut self,
name: N,
value: V,
overwrite: bool
) -> Result<&mut Self>
pub fn add_header<N, V>( &mut self, name: N, value: V, overwrite: bool ) -> Result<&mut Self>
Modify a header for this request.
When overwrite
is set to true
, If the header is already present, the value will be replaced.
When overwrite
is set to false
, The new header is always appended to the request, even if the header already exists.
sourcepub fn body_mut(&mut self) -> &mut ReqBody
pub fn body_mut(&mut self) -> &mut ReqBody
Returns a mutable reference to the associated HTTP body.
sourcepub fn replace_body(&mut self, body: ReqBody) -> ReqBody
pub fn replace_body(&mut self, body: ReqBody) -> ReqBody
Sets body to a new value and returns old value.
sourcepub fn take_body(&mut self) -> ReqBody
pub fn take_body(&mut self) -> ReqBody
Take body form the request, and set the body to None in the request.
sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Returns a reference to the associated extensions.
§Examples
let req = Request::default();
assert!(req.extensions().get::<i32>().is_none());
sourcepub async fn web_transport_mut(
&mut self
) -> Result<&mut WebTransportSession<Connection, Bytes>, Error>
Available on crate feature quinn
only.
pub async fn web_transport_mut( &mut self ) -> Result<&mut WebTransportSession<Connection, Bytes>, Error>
quinn
only.Try to get a WebTransport session from the request.
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 req = Request::default();
req.extensions_mut().insert("hello");
assert_eq!(req.extensions().get(), Some(&"hello"));
sourcepub fn first_accept(&self) -> Option<Mime>
pub fn first_accept(&self) -> Option<Mime>
Get first accept.
sourcepub fn content_type(&self) -> Option<Mime>
pub fn content_type(&self) -> Option<Mime>
Get content type.
Available on crate feature cookie
only.
cookie
only.Get CookieJar
reference.
Available on crate feature cookie
only.
cookie
only.Get CookieJar
mutable reference.
Available on crate feature cookie
only.
cookie
only.Get Cookie
from cookies.
sourcepub fn params_mut(&mut self) -> &mut IndexMap<String, String>
pub fn params_mut(&mut self) -> &mut IndexMap<String, String>
Get params mutable reference.
sourcepub fn param<'de, T>(&'de self, key: &str) -> Option<T>where
T: Deserialize<'de>,
pub fn param<'de, T>(&'de self, key: &str) -> Option<T>where
T: Deserialize<'de>,
Get param value from params.
sourcepub fn queries_mut(&mut self) -> &mut MultiMap<String, String>
pub fn queries_mut(&mut self) -> &mut MultiMap<String, String>
Get mutable queries reference.
sourcepub fn query<'de, T>(&'de self, key: &str) -> Option<T>where
T: Deserialize<'de>,
pub fn query<'de, T>(&'de self, key: &str) -> Option<T>where
T: Deserialize<'de>,
Get query value from queries.
sourcepub async fn form<'de, T>(&'de mut self, key: &str) -> Option<T>where
T: Deserialize<'de>,
pub async fn form<'de, T>(&'de mut self, key: &str) -> Option<T>where
T: Deserialize<'de>,
Get field data from form.
sourcepub async fn form_or_query<'de, T>(&'de mut self, key: &str) -> Option<T>where
T: Deserialize<'de>,
pub async fn form_or_query<'de, T>(&'de mut self, key: &str) -> Option<T>where
T: Deserialize<'de>,
Get field data from form, if key is not found in form data, then get from query.
sourcepub async fn query_or_form<'de, T>(&'de mut self, key: &str) -> Option<T>where
T: Deserialize<'de>,
pub async fn query_or_form<'de, T>(&'de mut self, key: &str) -> Option<T>where
T: Deserialize<'de>,
Get value from query, if key is not found in queries, then get from form.
sourcepub async fn file<'a>(&'a mut self, key: &'a str) -> Option<&'a FilePart>
pub async fn file<'a>(&'a mut self, key: &'a str) -> Option<&'a FilePart>
Get FilePart
reference from request.
sourcepub async fn first_file(&mut self) -> Option<&FilePart>
pub async fn first_file(&mut self) -> Option<&FilePart>
Get FilePart
reference from request.
sourcepub async fn files<'a>(&'a mut self, key: &'a str) -> Option<&'a Vec<FilePart>>
pub async fn files<'a>(&'a mut self, key: &'a str) -> Option<&'a Vec<FilePart>>
Get FilePart
list reference from request.
sourcepub async fn payload(&mut self) -> Result<&Bytes, ParseError>
pub async fn payload(&mut self) -> Result<&Bytes, ParseError>
Get request payload with default max size limit(64KB).
https://github.com/hyperium/hyper/issues/3111 *Notice: This method takes body.
sourcepub async fn payload_with_max_size(
&mut self,
max_size: usize
) -> Result<&Bytes, ParseError>
pub async fn payload_with_max_size( &mut self, max_size: usize ) -> Result<&Bytes, ParseError>
Get request payload with max size limit.
https://github.com/hyperium/hyper/issues/3111 *Notice: This method takes body.
sourcepub async fn form_data(&mut self) -> Result<&FormData, ParseError>
pub async fn form_data(&mut self) -> Result<&FormData, ParseError>
Get FormData
reference from request.
*Notice: This method takes body and body’s size is not limited.
sourcepub async fn extract<'de, T>(&'de mut self) -> Result<T, ParseError>
pub async fn extract<'de, T>(&'de mut self) -> Result<T, ParseError>
Extract request as type T
from request’s different parts.
sourcepub async fn extract_with_metadata<'de, T>(
&'de mut self,
metadata: &'de Metadata
) -> Result<T, ParseError>where
T: Deserialize<'de> + Send,
pub async fn extract_with_metadata<'de, T>(
&'de mut self,
metadata: &'de Metadata
) -> Result<T, ParseError>where
T: Deserialize<'de> + Send,
Extract request as type T
from request’s different parts.
sourcepub fn parse_params<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
pub fn parse_params<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
Parse url params as type T
from request.
sourcepub fn parse_queries<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
pub fn parse_queries<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
Parse queries as type T
from request.
sourcepub fn parse_headers<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
pub fn parse_headers<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
Parse headers as type T
from request.
Available on crate feature cookie
only.
cookie
only.Parse cookies as type T
from request.
sourcepub async fn parse_json<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
pub async fn parse_json<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
Parse json body as type T
from request with default max size limit.
sourcepub async fn parse_json_with_max_size<'de, T>(
&'de mut self,
max_size: usize
) -> Result<T, ParseError>where
T: Deserialize<'de>,
pub async fn parse_json_with_max_size<'de, T>(
&'de mut self,
max_size: usize
) -> Result<T, ParseError>where
T: Deserialize<'de>,
Parse json body as type T
from request with max size limit.
sourcepub async fn parse_form<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
pub async fn parse_form<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
Parse form body as type T
from request.
sourcepub async fn parse_body<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
pub async fn parse_body<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Deserialize<'de>,
Parse json body or form body as type T
from request with default max size.
sourcepub async fn parse_body_with_max_size<'de, T>(
&'de mut self,
max_size: usize
) -> Result<T, ParseError>where
T: Deserialize<'de>,
pub async fn parse_body_with_max_size<'de, T>(
&'de mut self,
max_size: usize
) -> Result<T, ParseError>where
T: Deserialize<'de>,
Parse json body or form body as type T
from request with max size.