Forward request body to response in Actix-Web

I would like to forward the Actix-Web request body to the response body (something like echo) but it gives a mismatched types error.

fn show_request(
    request: &actix_web::HttpRequest,
    request_type: RequestType,
) -> Box<Future<Item = HttpResponse, Error = Error>> {
    request
        .body()
        .from_err::<actix_web::error::PayloadError>()
        .map(move |f| {
            Box::new(ok(actix_web::HttpResponse::Ok()
                .content_type("text/plain")
                .body(f)))
        })
}

error:

error[E0308]: mismatched types
  --> src/proj.rs:50:2
   |
49 |   ) -> Box<Future<Item=HttpResponse, Error=Error>> {
   |        ------------------------------------------- expected `std::boxed::Box<(dyn futures::Future<Error=actix_web::Error, Item=actix_web::HttpResponse> + 'static)>` because of return type
50 |       request
   |  _____^
51 | |         .body()
52 | |         .from_err::<actix_web::error::PayloadError>()
53 | |         .map(move |f| {
...  |
56 | |                 .body(f)))
57 | |         })
   | |__________^ expected struct `std::boxed::Box`, found struct `futures::Map`
   |
   = note: expected type `std::boxed::Box<(dyn futures::Future<Error=actix_web::Error, Item=actix_web::HttpResponse> + 'static)>`
              found type `futures::Map<futures::future::FromErr<actix_web::dev::MessageBody<actix_web::HttpRequest>, actix_web::error::PayloadError>, [closure@src/.rs:53:8: 57:4]>`

Why does this error occur and how can I fix it?

Thanks.

You're saying you're going to return -> Box<Future<…>>, but you're not making the Box in your function.

This should do it:

let future = request.body().…;
Box::new(future);

And IIRC Actix has a .responder() method that also does it for you.

1 Like