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

≯℡__Kan透↙ 提交于 2020-08-05 07:44:06

问题


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);
}

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

curl returns the HTML I expected:

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

回答1:


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 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.



来源:https://stackoverflow.com/questions/60470052/why-does-the-reqwest-http-library-return-binary-data-instead-of-a-text-body

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!