pub struct Query<T>(pub T);Expand description
Extract typed information from the request’s query.
Note: A query string consists of unordered key=value pairs, therefore it cannot
be decoded into any type which depends upon data ordering e.g. tuples or tuple-structs.
Attempts to do so will fail at runtime.
If the query string cannot be deserialized into T, extraction fails with
QueryPayloadError, which the default
error domain renders as 400 Bad Request.
§Example
use ntex::web;
#[derive(Debug, serde::Deserialize)]
pub enum ResponseType {
Token,
Code
}
#[derive(serde::Deserialize)]
pub struct AuthRequest {
id: u64,
response_type: ResponseType,
}
// Use `Query` extractor for query information (and destructure it within the signature).
// If `id` or `response_type` is missing or invalid, extraction fails and the
// handler is not called.
// The correct request for this handler would be `/index.html?id=64&response_type=Code`.
async fn index(web::types::Query(info): web::types::Query<AuthRequest>) -> String {
format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type)
}
fn main() {
let app = web::App::default().service(
web::resource("/index.html").route(web::get().to(index))); // <- use `Query` extractor
}Tuple Fields§
§0: TImplementations§
Source§impl<T> Query<T>
impl<T> Query<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Deconstruct to a inner value
Sourcepub fn from_query(query_str: &str) -> Result<Self, QueryPayloadError>where
T: DeserializeOwned,
pub fn from_query(query_str: &str) -> Result<Self, QueryPayloadError>where
T: DeserializeOwned,
Get query parameters from the path
Trait Implementations§
impl<T: Eq> Eq for Query<T>
Source§impl<St, T> FromRequest<St> for Query<T>where
St: State,
T: DeserializeOwned,
Extract typed information from the request’s query.
impl<St, T> FromRequest<St> for Query<T>where
St: State,
T: DeserializeOwned,
Extract typed information from the request’s query.
§Example
use ntex::web;
#[derive(Debug, serde::Deserialize)]
pub enum ResponseType {
Token,
Code
}
#[derive(serde::Deserialize)]
pub struct AuthRequest {
id: u64,
response_type: ResponseType,
}
// Use `Query` extractor for query information.
// If `id` or `response_type` is missing or invalid, extraction fails and the
// handler is not called.
// The correct request for this handler would be `/index.html?id=64&response_type=Code`
async fn index(info: web::types::Query<AuthRequest>) -> String {
format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type)
}
fn main() {
let app = web::App::default().service(
web::resource("/index.html")
.route(web::get().to(index))); // <- use `Query` extractor
}Source§type Error = QueryPayloadError
type Error = QueryPayloadError
The error returned when extraction fails. Read more
Source§async fn from_request(
_: &St,
req: &HttpRequest,
_: &mut Payload,
) -> Result<Self, Self::Error>
async fn from_request( _: &St, req: &HttpRequest, _: &mut Payload, ) -> Result<Self, Self::Error>
Creates the extractor value for this request.
Source§impl<T: Ord> Ord for Query<T>
impl<T: Ord> Ord for Query<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<T: PartialOrd> PartialOrd for Query<T>
impl<T: PartialOrd> PartialOrd for Query<T>
impl<T: PartialEq> StructuralPartialEq for Query<T>
Auto Trait Implementations§
impl<T> Freeze for Query<T>where
T: Freeze,
impl<T> RefUnwindSafe for Query<T>where
T: RefUnwindSafe,
impl<T> Send for Query<T>where
T: Send,
impl<T> Sync for Query<T>where
T: Sync,
impl<T> Unpin for Query<T>where
T: Unpin,
impl<T> UnsafeUnpin for Query<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Query<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.