How to return ResponseFuture? (Actix)

Hello.
I would like to check the string "option" (from the http request) and exit the function with an error.
How to do it?

//actix = "0.7"
//actix-web = "0.7"

pub fn get_vars(req: HttpRequest<State>) -> ResponseFuture
{
    let state = &req.state().get();
    let info = req.match_info();
     
    let enum = match info.get("option")
    {
        Some("A") => MyEnum::A,
        Some("B") => MyEnum:B,
        _ => {
            //HERE: return ERROR!
            // return ResponseFuture::new(); ??
        },
    };
   
    // any code
}

let web_app = move || {
        App::with_state(MyState::new())
            .middleware(Logger::default())
            .route("/vars/{option}", http::Method::GET, get_vars)
};

HttpServer::new(web_app).bind("0.0.0.0:8080").unwrap().start();

You're using an old version of Actix where this was more complicated. If you switch to Actix v2/v3, it's as easy as changing fn to async fn.

In 0.7 and v1, IIRC, ResponseFuture is not a type, but an alias for a Box of Future that returns actix Response. You need something like futures::future::ok(ResponseBuilder::new().body(())) or futures::future::ok(()).responder().

doesn't compile.

   |
40 | pub fn get_vars(req: HttpRequest<State>) -> ResponseFuture
   |                                             -------------- expected `std::boxed::Box<(dyn futures::Future<Item = actix_web::HttpResponse, Error = actix_web::Error> + 'static)>` because of return type
...
46 |     futures::future::ok(ResponseBuilder::new().body(()))
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |
   |     expected struct `std::boxed::Box`, found struct `futures::Done`
   |     help: store this in the heap by calling `Box::new`: `Box::new(futures::future::ok(ResponseBuilder::new().body(())))`
   |
   = note: expected struct `std::boxed::Box<(dyn futures::Future<Item = actix_web::HttpResponse, Error = actix_web::Error> + 'static)>`
              found struct `futures::Done<_, _>`

Add .boxed() too. Or Box::new() if boxed isn't in scope for some reason.

Do you have a reason to battle with the old and dificult Actix 0.7? Rewrite in Actix v3 may be easier than solving the old fiddly problems of experimental and abandoned version of futures.

I solved this problem:

  return Box::new(future::ok(HttpResponse::Ok().finish()));

Thx, @kornel.

OK. I'm migrating to the latest version.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.