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 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.
Examples
let mut req: Request= Request::default();
*req.uri_mut() = "/hello".parse().unwrap();
assert_eq!(*req.uri(), *"/hello");
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 remote_addr(&self) -> Option<&SocketAddr>
pub fn remote_addr(&self) -> Option<&SocketAddr>
Get request remote address.
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<()>where
N: IntoHeaderName,
V: TryInto<HeaderValue>,
pub fn add_header<N, V>(
&mut self,
name: N,
value: V,
overwrite: bool
) -> Result<()>where
N: IntoHeaderName,
V: TryInto<HeaderValue>,
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 with_header<N, V>(
&mut self,
name: N,
value: V,
overwrite: bool
) -> Result<&mut Self>where
N: IntoHeaderName,
V: TryInto<HeaderValue>,
pub fn with_header<N, V>(
&mut self,
name: N,
value: V,
overwrite: bool
) -> Result<&mut Self>where
N: IntoHeaderName,
V: TryInto<HeaderValue>,
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(&self) -> Option<&ReqBody>
pub fn body(&self) -> Option<&ReqBody>
Returns a reference to the associated HTTP body.
Examples
let req = Request::default();
assert!(req.body().is_some());
sourcepub fn body_mut(&mut self) -> Option<&mut ReqBody>
pub fn body_mut(&mut self) -> Option<&mut ReqBody>
Returns a mutable reference to the associated HTTP body.
sourcepub fn take_body(&mut self) -> Option<ReqBody>
pub fn take_body(&mut self) -> Option<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 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 HashMap<String, String>
pub fn params_mut(&mut self) -> &mut HashMap<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 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 all_files(&mut self) -> Vec<&FilePart> ⓘ
pub async fn all_files(&mut self) -> Vec<&FilePart> ⓘ
Get FilePart
list reference from request.
sourcepub async fn payload(&mut self) -> Result<&Vec<u8>, ParseError>
pub async fn payload(&mut self) -> Result<&Vec<u8>, ParseError>
Get request payload.
*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.
sourcepub async fn extract<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Extractible<'de>,
pub async fn extract<'de, T>(&'de mut self) -> Result<T, ParseError>where
T: Extractible<'de>,
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>,
pub async fn extract_with_metadata<'de, T>(
&'de mut self,
metadata: &'de Metadata
) -> Result<T, ParseError>where
T: Deserialize<'de>,
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.
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.