首页 > 解决方案 > 为什么 reqwest HTTP 库返回二进制数据而不是文本正文?

问题描述

我正在尝试使用 reqwest 执行 HTTP GET 请求并将响应正文打印到 STDOUT。这适用于大多数网站,但它会为 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()返回二进制数据,如何从中获取正常的 HTTP 正文?

curl 返回我期望的 HTML:

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

标签: httprustreqwest

解决方案


如果你这样做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从 reqwest结帐。gzip是一个可选功能,请参阅cargo doc,对于 reqwest 你可以写reqwest = { version = "0.10.3", features = ["gzip"] }在你的Cargo.toml.


推荐阅读