本文介绍了为什么reqwest HTTP库返回二进制数据而不是文本主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用reqwest执行HTTP GET请求,并将响应正文打印到STDOUT.这适用于大多数网站,但会为amazon.com返回奇怪的二进制输出:

I am trying to perform a HTTP GET request with reqwest and print the response body to STDOUT. This works for most websites, but it returns weird binary output for 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正文?

Why would resp.text().await.unwrap() return binary data and how can I obtain normal HTTP body from it?

curl返回我期望的HTML:

curl returns the HTML I expected:

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,您将看到:

If you do curl https://www.amazon.com/PNY-GeForce-Gaming-Overclocked-Graphics/dp/B07GJ7TV8L/ - I you will see:

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很明显,您需要执行什么操作.从reqwest检出 gzip . gzip可选功能,请参见货运文档,对于reqwest,您可以编写reqwest = { version = "0.10.3", features = ["gzip"] }在您的Cargo.toml中.

content-encoding: gzip it's quite obvious what you need to do. Checkout gzip from reqwest. gzip is a optional features, see cargo doc, for reqwest you can write reqwest = { version = "0.10.3", features = ["gzip"] } in your Cargo.toml.

这篇关于为什么reqwest HTTP库返回二进制数据而不是文本主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 16:22