http-rs / surf

Fast and friendly HTTP client framework for async Rust
https://docs.rs/surf
Apache License 2.0
1.45k stars 119 forks source link

hyper-client feature requires curl-client feature to work #275

Closed stone-bits closed 3 years ago

stone-bits commented 3 years ago

I specify hyper-client as http backend.

[dependencies]
surf =  { version = "2.1", default-features = false, features = ["hyper-client", "encoding"] }
tokio = { version = "1", features = ["full"] }
use surf;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let req = surf::get("https://httpbin.org/get");
    let resp = surf::client().send(req).await;
    println!("Resp: {:?}", resp);
    Ok(())
}

But, unfortunately, that does not work: thread 'main' panicked at 'not currently running on the Tokio runtime.'

When I add curl-client to features: surf = { version = "2.1", default-features = false, features = ["hyper-client", "encoding", "curl-client"] }

It works.

But it seems like I have many unnecessary dependencies now (curl, isahc, etc.) that enabled by curl-client feature.

Can hyper-client work without curl-client?

Fishrock123 commented 3 years ago

Odd. That seems like a bug, not sure from where or why though.

Fishrock123 commented 3 years ago

@igor-tk Are you initializing the tokio runtime anywhere? You may need to do that, Surf probably won't do that for you but isahc may internally.

stone-bits commented 3 years ago

I set #[tokio::main] There is no other code. It's isolated short example. Without curl-client it does not work. With curl-client option it works using hyper and tokio.

Fishrock123 commented 3 years ago

This is very strange, since isahc does not use tokio directly at all: https://github.com/sagebind/isahc/blob/master/Cargo.toml

I honestly have no idea.

The ci improvements I merged yesterday include hyper in the ci matrix and it seems to work: https://github.com/http-rs/surf/pull/263

Fishrock123 commented 3 years ago

I think this should be fixed if you do cargo update, see https://github.com/http-rs/http-client/commit/ec039a45f4c793b5e2ee7c996a3dcdeeee2b78cd

jessbowers commented 9 months ago

Still getting this error with:

surf = { version = "2.3.2", default-features = false, features = [
    "hyper-client",
    "middleware-logger",
    "encoding",
] }
thread 'main' panicked at 'there is no timer running, must be called from the context of a Tokio 0.2.x runtime', /Users/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-0.2.25/src/time/driver/handle.rs:24:32