ServiceRequest as parameter for handler #2916
-
A request handler async function can take many different parameters (12) that are Did I miss it, is there a way to take ServiceRequest as a parameter in request handler? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
There's no way to use ServiceRequest in this way. Instead you can use: async fn handler(req: HttpRequest, pl: web::Payload) -> actix_web::Result<impl Responder> {
...
Form::from_request(&req, &mut pl.into_inner()).await?
...
} |
Beta Was this translation helpful? Give feedback.
-
Thank you. use actix_utils::future::Ready;
use actix_web::error::Error;
pub struct RequestAndPayload{
req: actix_web::HttpRequest,
pl: actix_web::dev::Payload,
}
impl actix_web::FromRequest for RequestAndPayload {
type Error = Error;
type Future = Ready<Result<Self, Error>>;
#[inline]
fn from_request(
req: &actix_web::HttpRequest,
pl: &mut actix_web::dev::Payload,
) -> Self::Future {
let rap = RequestAndPayload{
req : req.to_owned(),
pl : pl.take(),
};
actix_utils::future::ok(rap)
}
} |
Beta Was this translation helpful? Give feedback.
Thank you.
Is it possible to use only one parameter instead of two ?
Maybe a new struct
RequestAndPayload
with both fields andimpl FromRequest
?Could this work?