How to resolve the client future?


#1

I am trying to send a request to a server with data using hyper client. The way I am doing it is

fn send_request(remote: Remote) -> Result<hyper::Response, futures::Canceled> {
    let (tx, rx) = futures::oneshot();
    remote.spawn(|handle| {
        let client = Client::new(&handle);
        let uri = "http://localhost:8000".parse().unwrap();
        client.get(uri).map_err(|_err| ()).and_then(|resp| {
            println!("Never prints out");
            tx.send(resp).unwrap();
            Ok(())
        }).or_else(|_err| {
            Err(())
        })
    });
    rx.wait()
}

But send_request never returns because client.get(url).and_then(...) is never resolved (found out through print debugging).

The examples on Hyper site call core.run(work) at the end to get the response. But I really can’t use Core here because it cannot be shared across multiple threads. Is there any other way I can get the client.get(uri).and_then(...) future to run to completion?


#2

What about an error? and_then runs on a successful resolution, but you don’t seem to send anything on the rx channel in case of error.


#3

Here’s working example. Hope it helps.


#4

Does this actually work for you? It is stuck just like my program is on my laptop and prints nothing. I wonder why!


#5

Here’s log from travis: https://travis-ci.org/yjh0502/futuretest/jobs/271758672#L601
And source code: https://github.com/yjh0502/futuretest/blob/master/src/lib.rs


#6

Thanks a bunch! It must be a network issue on my side and that’s why the surprise. Thanks again.