HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustMajor

could not find `blocking` in `reqwest`

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
couldblockingfindnotreqwest

Problem

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*** "]
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);

Solution

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.

Code Snippets

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

Context

Stack Overflow Q#58906965, score: 99

Revisions (0)

No revisions yet.