Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ch8/ch8-simple: reqwest fails to connect with rust 1.66: Invalid argument #98

Open
ewenmcneill opened this issue Jan 9, 2023 · 0 comments

Comments

@ewenmcneill
Copy link

With the current stable rust (1.66; on macOS 11.7.1), the ch8/ch8-simple example does not run (at least on a system with IPv4 and IPv6 addresses on the client), with a typical run attempt ending with:

   Compiling ch8-simple v0.1.0 (/Users/ewen/misc/src/rust/rust-in-action/code/ch8/ch8-simple)
    Finished dev [unoptimized + debuginfo] target(s) in 26.35s
     Running `target/debug/ch8-simple`
Error: Error(Hyper(Error(Connect, Os { code: 22, kind: InvalidInput, message: "Invalid argument" })), "http://www.rustinaction.com/")

After trying a few other example websites sites (my own, http://httpforever.com/ in case it was the HTTPS redirect, etc) I eventually concluded I wasn't going to get the very old reqwest version (0.9) working on rust 1.66 on my system. FTR, I managed to get a few different errors depending on the site, including Os { code: 47, kind: Uncategorized, message: "Address family not supported by protocol family" } which I suspect is caused by trying to connect to IPv6 addresses via IPv4 sockets or similar, but nothing that seemed likely to work at all.

So I forward ported the example to reqwest 0.11, which now defaults to an asynchronous API (since 0.10.0), but does have an optional "blocking" API that works more like reqwest 0.9 still available if you explicitly depend on it.

In case someone else needs to do this, the change required are:

  1. In Cargo.toml change the dependency to reqwest = { version = "0.11.13", features = ["blocking"] }

  2. In src/main.rs change the call to the API to let response = reqwest::blocking::get(url)?; (the mut that was there removed because the compiler told me it wasn't necessary; possibly also a reqwest 0.11 API change)

FTR, I'm not making this a PR as I'm unclear how far back in rust versions reqwest 0.10 / 0.11 support, and this may just be a backwards / forwards incompatibility issue ("stable" rust isn't that stable, still). OTOH, reqwest 0.10.0, which introduced this feature was released over 3 years ago, so mabye it's time to update the example.

Either way, this is a New And Exciting (tm) way that the included examples don't work...

... and if this repo is every updated again, mabye it'd be worth updating to the reqwest 0.11 syntax, with a comment in the source in the repo about why it differs from the book text.

git diff of changes to use reqwest 0.11
ewen@basadi:~/misc/src/rust/rust-in-action/code/ch8/ch8-simple$ git diff Cargo.toml src
diff --git a/ch8/ch8-simple/Cargo.toml b/ch8/ch8-simple/Cargo.toml
index bb8e938..26fd1c7 100644
--- a/ch8/ch8-simple/Cargo.toml
+++ b/ch8/ch8-simple/Cargo.toml
@@ -5,5 +5,4 @@ authors = ["Tim McNamara <author@rustinaction.com>"]
 edition = "2018"
 
 [dependencies]
-reqwest = "0.9"
-
+reqwest = { version = "0.11.13", features = ["blocking"] }
diff --git a/ch8/ch8-simple/src/main.rs b/ch8/ch8-simple/src/main.rs
index 3ac5dc3..1038049 100644
--- a/ch8/ch8-simple/src/main.rs
+++ b/ch8/ch8-simple/src/main.rs
@@ -4,7 +4,7 @@ use reqwest;
 
 fn main() -> Result<(), Box<dyn Error>> {        // <1>
   let url = "http://www.rustinaction.com/";
-  let mut response = reqwest::get(url)?;
+  let response = reqwest::blocking::get(url)?;
 
   let content = response.text()?;
   print!("{}", content);
ewen@basadi:~/misc/src/rust/rust-in-action/code/ch8/ch8-simple$ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant