Struct Query

pub struct Query<T>(pub T);

Extractor that deserializes query strings into some type.

T is expected to implement serde::Deserialize.

Examples

use axum::{
    extract::Query,
    routing::get,
    Router,
};
use serde::Deserialize;

#[derive(Deserialize)]
struct Pagination {
    page: usize,
    per_page: usize,
}

// This will parse query strings like `?page=2&per_page=30` into `Pagination`
// structs.
async fn list_things(pagination: Query<Pagination>) {
    let pagination: Pagination = pagination.0;

    // ...
}

let app = Router::new().route("/list_things", get(list_things));
# let _: Router = app;

If the query string cannot be parsed it will reject the request with a 400 Bad Request response.

For handling values being empty vs missing see the query-params-with-empty-strings example.

For handling multiple values for the same query parameter, in a ?foo=1&foo=2&foo=3 fashion, use axum_extra::extract::Query instead.

Fields

0: T

Implementations

impl<T> Query<T> where T: DeserializeOwned,

fn try_from_uri(value: &Uri) -> Result<Self, QueryRejection>

Attempts to construct a Query from a reference to a Uri.

Example

use axum::extract::Query;
use http::Uri;
use serde::Deserialize;

#[derive(Deserialize)]
struct ExampleParams {
    foo: String,
    bar: u32,
}

let uri: Uri = "http://example.com/path?foo=hello&bar=42".parse().unwrap();
let result: Query<ExampleParams> = Query::try_from_uri(&uri).unwrap();
assert_eq!(result.foo, String::from("hello"));
assert_eq!(result.bar, 42);

Trait Implementations

impl<T> Deref for Query<T>

type Target = T;
fn deref(&self) -> &Self::Target

impl<T> DerefMut for Query<T>

fn deref_mut(&mut self) -> &mut Self::Target

impl<T, S> FromRequestParts<S> for Query<T> where T: DeserializeOwned, S: Send + Sync,

type Rejection = QueryRejection;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection>

impl<T: Clone> Clone for Query<T>

fn clone(&self) -> Self

impl<T: Copy> Copy for Query<T>

impl<T: Debug> Debug for Query<T>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T: Default> Default for Query<T>

fn default() -> Self

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

impl<P, T> Receiver for Query<T> where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

impl<R> Rng for Query<T> where R: RngCore + ?Sized,

impl<R> TryCryptoRng for Query<T> where R: CryptoRng + ?Sized,

impl<R> TryRngCore for Query<T> where R: RngCore + ?Sized,

type Error = never;
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), <R as TryRngCore>::Error>

impl<S, T> FromRequest<S, ViaParts> for Query<T> where S: Send + Sync, T: FromRequestParts<S>,

type Rejection = <T as FromRequestParts<S>>::Rejection;
fn from_request(req: Request<Body>, state: &S) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for Query<T> where ST: ?Sized, DT: ?Sized,

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for Query<T> where ST: ?Sized, DT: ?Sized,

impl<T> Any for Query<T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Query<T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Query<T> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Query<T> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> CryptoRng for Query<T> where T: DerefMut, <T as Deref>::Target: CryptoRng,

impl<T> From<T> for Query<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> FromRef<T> for Query<T> where T: Clone,

fn from_ref(input: &T) -> T

impl<T> Instrument for Query<T>

impl<T> Read<Exclusive, BecauseExclusive> for Query<T> where T: ?Sized,

impl<T> RngCore for Query<T> where T: DerefMut, <T as Deref>::Target: RngCore,

fn next_u32(&mut self) -> u32
fn next_u64(&mut self) -> u64
fn fill_bytes(&mut self, dst: &mut [u8])

impl<T> Same for Query<T>

type Output = T;

impl<T> ToOwned for Query<T> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> WithSubscriber for Query<T>

impl<T, U> Into<U> for Query<T> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Query<T> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Query<T> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

impl<V, T> VZip<V> for Query<T> where V: MultiLane<T>,

fn vzip(self) -> V