Trait gotham::extractor::QueryStringExtractor
source · [−]pub trait QueryStringExtractor<B>: for<'de> Deserialize<'de> + StaticResponseExtender<ResBody = B> + StateData where
B: HttpBody, { }
Expand description
Defines a binding for storing the query parameters from the Request
URI in State
. On
failure the StaticResponseExtender
implementation extends the Response
to indicate why the
extraction process failed.
This trait is automatically implemented when the struct implements the Deserialize
,
StateData
and StaticResponseExtender
traits. These traits can be derived, or implemented
manually for greater control.
The default behaviour given by deriving all three traits will use the automatically derived
behaviour from Serde, and result in a 400 Bad Request
HTTP response if the query string is
not able to be deserialized.
Examples
#[derive(Deserialize, StateData, StaticResponseExtender)]
struct MyQueryParams {
x: i32,
y: MyEnum,
}
#[derive(Deserialize, Clone, Copy, Debug)]
#[serde(rename_all = "kebab-case")]
enum MyEnum {
A,
B,
C,
}
fn handler(state: State) -> (State, Response<Body>) {
let &MyQueryParams { x, y } = MyQueryParams::borrow_from(&state);
let body = format!("x = {}, y = {:?}", x, y);
let response = create_response(
&state,
StatusCode::OK,
mime::TEXT_PLAIN,
body,
);
(state, response)
}
fn router() -> Router {
build_simple_router(|route| {
route
.get("/test")
.with_query_string_extractor::<MyQueryParams>()
.to(handler);
})
}