ValidateRequest example not working in axum #346
-
Hi! I took the example error[E0271]: type mismatch resolving `<Route as Service<Request<Body>>>::Response == Response<Body>`
--> simple-rest/src/main.rs:92:16
|
92 | .layer(ValidateRequestHeaderLayer::custom(MyHeader {}))
| ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `Body`, found struct `UnsyncBoxBody`
| |
| required by a bound introduced by this call let app = Router::new()
.layer(ValidateRequestHeaderLayer::custom(MyHeader {})) use tower_http::validate_request::{ValidateRequestHeaderLayer, ValidateRequest};
use hyper::{Request, Response, Body, Error};
use http::{StatusCode, header::ACCEPT};
use tower::{Service, ServiceExt, ServiceBuilder, service_fn};
#[derive(Clone, Copy)]
pub struct MyHeader { /* ... */ }
impl<B> ValidateRequest<B> for MyHeader {
type ResponseBody = Body;
fn validate(
&mut self,
request: &mut Request<B>,
) -> Result<(), Response<Self::ResponseBody>> {
// validate the request...
Ok(())
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
axum requires all response bodies to be |
Beta Was this translation helpful? Give feedback.
-
Is there another way to solve the issue to make the middleware not axum specific? |
Beta Was this translation helpful? Give feedback.
-
Thanks, that did the trick! |
Beta Was this translation helpful? Give feedback.
axum requires all response bodies to be
axum::body::BoxBody
. Usingtype ResponseBody = axum::body::BoxBody;
instead should work. You can create a box body withboxed