Skip to content

ValidateRequest example not working in axum #345

@lcmgh

Description

@lcmgh

Hi!

I took the example ValidateRequest impl from https://docs.rs/tower-http/latest/tower_http/validate_request/index.html but when using it with Axum it fails while the provided implementations work out of the box.

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(())
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions