52

I am trying to download a text file from a given URL using reqwest 0.10.0-alpha.2, which looks like an appropriate tool. I have this in my Cargo.toml file:

[package]
name = "..."
version = "0.1.0"
authors = ["Y*** <y***@***.***>"]
edition = "2019"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = "0.10.0-alpha.2"

The dependency seems to resolve and I have my Cargo.lock file.

I have this snippet lifted from the docs

let body = reqwest::blocking::get("https://www.rust-lang.org")?
    .text()?;

println!("body = {:?}", body);

But I am getting this error:

  |  
  |     let body = reqwest::blocking::get("https://www.rust-lang.org")?.text()?;  
  |                         ^^^^^^^^ could not find `blocking` in `reqwest`  

Why? I do see this line on the doc "This requires the optional blocking feature to be enabled" from the above link. It might be just that. However, it is not clear to me how you enable a "feature" for a library in Rust either.


I also tried this (some shooting in the dark):

use reqwest::blocking;

Same error:

 |
 | use reqwest::blocking;
 |     ^^^^^^^^^^^^^^^^^ no `blocking` in the root

Following @edwardw's answer to enable "blocking" in "reqwest", and then also have to change ? to unwrap. Not sure, but maybe ? is from an older version of rust or sth. But it doesn't compile for me.

let body = reqwest::blocking::get("https://www.rust-lang.org")
    .unwrap()
    .text();
println!("body = {:?}", body);
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • 1
    Seems like reqwest not documented well: https://www.gitmemory.com/issue/seanmonstar/reqwest/643/532880897 – num8er Nov 18 '19 at 03:03

1 Answers1

79

It is an optional feature of the crate. You have to explicitly enable it in dependencies:

[dependencies]
reqwest = { version = "0.10.0-alpha.2", features = ["blocking"] }

The reqwest::blocking documentation does mention it:

This requires the optional blocking feature to be enabled.

edwardw
  • 12,652
  • 3
  • 40
  • 51