Trait catalyzer::internals::crates::axum::RequestExt
pub trait RequestExt: Sized + Sealed {
// Required methods
fn extract<E, M>(
self
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequest<(), M>>::Rejection>> + Send>>
where E: FromRequest<(), M> + 'static,
M: 'static;
fn extract_with_state<E, S, M>(
self,
state: &S
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequest<S, M>>::Rejection>> + Send + '_>>
where E: FromRequest<S, M> + 'static,
S: Send + Sync;
fn extract_parts<E>(
&mut self
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<()>>::Rejection>> + Send + '_>>
where E: FromRequestParts<()> + 'static;
fn extract_parts_with_state<'a, E, S>(
&'a mut self,
state: &'a S
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<S>>::Rejection>> + Send + 'a>>
where E: FromRequestParts<S> + 'static,
S: Send + Sync;
fn with_limited_body(self) -> Request<Body>;
fn into_limited_body(self) -> Body;
}
Expand description
Extension trait that adds additional methods to Request
.
Required Methods§
fn extract<E, M>(
self
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequest<(), M>>::Rejection>> + Send>>where
E: FromRequest<(), M> + 'static,
M: 'static,
fn extract<E, M>(
self
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequest<(), M>>::Rejection>> + Send>>where
E: FromRequest<(), M> + 'static,
M: 'static,
Apply an extractor to this Request
.
This is just a convenience for E::from_request(req, &())
.
Note this consumes the request. Use RequestExt::extract_parts
if you’re not extracting
the body and don’t want to consume the request.
§Example
use axum::{
async_trait,
extract::{Request, FromRequest},
body::Body,
http::{header::CONTENT_TYPE, StatusCode},
response::{IntoResponse, Response},
Form, Json, RequestExt,
};
struct FormOrJson<T>(T);
#[async_trait]
impl<S, T> FromRequest<S> for FormOrJson<T>
where
Json<T>: FromRequest<()>,
Form<T>: FromRequest<()>,
T: 'static,
S: Send + Sync,
{
type Rejection = Response;
async fn from_request(req: Request, _state: &S) -> Result<Self, Self::Rejection> {
let content_type = req
.headers()
.get(CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.ok_or_else(|| StatusCode::BAD_REQUEST.into_response())?;
if content_type.starts_with("application/json") {
let Json(payload) = req
.extract::<Json<T>, _>()
.await
.map_err(|err| err.into_response())?;
Ok(Self(payload))
} else if content_type.starts_with("application/x-www-form-urlencoded") {
let Form(payload) = req
.extract::<Form<T>, _>()
.await
.map_err(|err| err.into_response())?;
Ok(Self(payload))
} else {
Err(StatusCode::BAD_REQUEST.into_response())
}
}
}
fn extract_with_state<E, S, M>(
self,
state: &S
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequest<S, M>>::Rejection>> + Send + '_>>
fn extract_with_state<E, S, M>( self, state: &S ) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequest<S, M>>::Rejection>> + Send + '_>>
Apply an extractor that requires some state to this Request
.
This is just a convenience for E::from_request(req, state)
.
Note this consumes the request. Use RequestExt::extract_parts_with_state
if you’re not
extracting the body and don’t want to consume the request.
§Example
use axum::{
async_trait,
body::Body,
extract::{Request, FromRef, FromRequest},
RequestExt,
};
struct MyExtractor {
requires_state: RequiresState,
}
#[async_trait]
impl<S> FromRequest<S> for MyExtractor
where
String: FromRef<S>,
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
let requires_state = req.extract_with_state::<RequiresState, _, _>(state).await?;
Ok(Self { requires_state })
}
}
// some extractor that consumes the request body and requires state
struct RequiresState { /* ... */ }
#[async_trait]
impl<S> FromRequest<S> for RequiresState
where
String: FromRef<S>,
S: Send + Sync,
{
// ...
}
fn extract_parts<E>(
&mut self
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<()>>::Rejection>> + Send + '_>>where
E: FromRequestParts<()> + 'static,
fn extract_parts<E>(
&mut self
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<()>>::Rejection>> + Send + '_>>where
E: FromRequestParts<()> + 'static,
Apply a parts extractor to this Request
.
This is just a convenience for E::from_request_parts(parts, state)
.
§Example
use axum::{
async_trait,
extract::{Path, Request, FromRequest},
response::{IntoResponse, Response},
body::Body,
Json, RequestExt,
};
use axum_extra::{
TypedHeader,
headers::{authorization::Bearer, Authorization},
};
use std::collections::HashMap;
struct MyExtractor<T> {
path_params: HashMap<String, String>,
payload: T,
}
#[async_trait]
impl<S, T> FromRequest<S> for MyExtractor<T>
where
S: Send + Sync,
Json<T>: FromRequest<()>,
T: 'static,
{
type Rejection = Response;
async fn from_request(mut req: Request, _state: &S) -> Result<Self, Self::Rejection> {
let path_params = req
.extract_parts::<Path<_>>()
.await
.map(|Path(path_params)| path_params)
.map_err(|err| err.into_response())?;
let Json(payload) = req
.extract::<Json<T>, _>()
.await
.map_err(|err| err.into_response())?;
Ok(Self { path_params, payload })
}
}
fn extract_parts_with_state<'a, E, S>(
&'a mut self,
state: &'a S
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<S>>::Rejection>> + Send + 'a>>
fn extract_parts_with_state<'a, E, S>( &'a mut self, state: &'a S ) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<S>>::Rejection>> + Send + 'a>>
Apply a parts extractor that requires some state to this Request
.
This is just a convenience for E::from_request_parts(parts, state)
.
§Example
use axum::{
async_trait,
extract::{Request, FromRef, FromRequest, FromRequestParts},
http::request::Parts,
response::{IntoResponse, Response},
body::Body,
Json, RequestExt,
};
struct MyExtractor<T> {
requires_state: RequiresState,
payload: T,
}
#[async_trait]
impl<S, T> FromRequest<S> for MyExtractor<T>
where
String: FromRef<S>,
Json<T>: FromRequest<()>,
T: 'static,
S: Send + Sync,
{
type Rejection = Response;
async fn from_request(mut req: Request, state: &S) -> Result<Self, Self::Rejection> {
let requires_state = req
.extract_parts_with_state::<RequiresState, _>(state)
.await
.map_err(|err| err.into_response())?;
let Json(payload) = req
.extract::<Json<T>, _>()
.await
.map_err(|err| err.into_response())?;
Ok(Self {
requires_state,
payload,
})
}
}
struct RequiresState {}
#[async_trait]
impl<S> FromRequestParts<S> for RequiresState
where
String: FromRef<S>,
S: Send + Sync,
{
// ...
}
fn with_limited_body(self) -> Request<Body>
fn with_limited_body(self) -> Request<Body>
Apply the default body limit.
If it is disabled, the request is returned as-is.
fn into_limited_body(self) -> Body
fn into_limited_body(self) -> Body
Consumes the request, returning the body wrapped in [http_body_util::Limited
] if a
default limit is in place, or not wrapped if the
default limit is disabled.