为什么 reqwest HTTP 库 return 二进制数据而不是文本正文?

Why does the reqwest HTTP library return binary data instead of a text body?

我正在尝试使用 reqwest 执行 HTTP GET 请求并将响应正文打印到 STDOUT。这适用于大多数网站,但 return 的 amazon.com:

的奇怪二进制输出
#[tokio::main]
async fn main() {
    run().await;
}

async fn run() {
    let url = "https://www.amazon.com/PNY-GeForce-Gaming-Overclocked-Graphics/dp/B07GJ7TV8L/";
    let resp = reqwest::get(url).await.unwrap();
    let text = resp.text().await.unwrap();
    println!("{}", text);
}

为什么 resp.text().await.unwrap() return 二进制数据以及如何从中获取正常的 HTTP 正文?

curl return我预期的 HTML:

curl https://www.amazon.com/PNY-GeForce-Gaming-Overclocked-Graphics/dp/B07GJ7TV8L/

如果你这样做 curl https://www.amazon.com/PNY-GeForce-Gaming-Overclocked-Graphics/dp/B07GJ7TV8L/ - I 你会看到:

server: Server
content-type: text/html
content-length: 2148
content-encoding: gzip
x-amz-rid: 2T9PBCY66S79SMC424V2
vary: Accept-Encoding
akamai-cache-status: Miss
date: Sat, 29 Feb 2020 22:23:54 GMT

content-encoding: gzip 很明显您需要做什么。签出 gzip from reqwest. gzip is a optional features, see cargo doc,对于 reqwest,您可以在 Cargo.toml.

中写入 reqwest = { version = "0.10.3", features = ["gzip"] }