2

我正在尝试解析来自 GET 请求的响应,但输出似乎是随机字节。

仅当我尝试此网站 ( https://api.color.pizza/v1/123123 ) 时才会出现此问题,并且尝试其他网站时响应符合预期。

在浏览器中进行测试,我知道正确的响应,并且在比较正确响应的字节(存储在变量中k)和来自 reqwest 的响应时,字节完全不同。

重现错误的代码如下:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    //Expected output
    let k = r##"{"colors":[{"hex":"#123120","name":"Méi Hēi Coal","rgb":{"r":18,"g":49,"b":32},"requestedHex":"#123123","luminance":29.48870965301805,"distance":3}]}"##;

    let request_url = "https://api.color.pizza/v1/123123";
    let resp = reqwest::blocking::get(request_url)?.text()?;

    println!("{}", resp);

    let r1 = resp.into_bytes();
    let r2 = String::from(k).into_bytes();
    assert_eq!(&r1[..], &r2[..]);

    Ok(())
}

输出是:

�VJ���/*V���V�H�P�RR642642P�Q�K�M
�^���qdr��s~bP�(=IɪZ�H���BG)]���RG  (dlT
                                            �L-,M-.IM�@2��+�473/1/h�������������������RJfq  DҸ6�X���
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `[31, 239, 191, 189, 8, 0, 0, 0, 0, 0, 0, 3, 239, 191, 189, 86, 74, 239, 191, 189, 239, 191, 189, 239, 191, 189, 47, 42, 86, 239, 191, 189, 239, 191, 189, 239, 191, 189, 86, 239, 191, 189, 72, 239, 191, 189, 80, 239, 191, 189, 82, 82, 54, 52, 50, 54, 52, 50, 80, 239, 191, 189, 81, 239, 191, 189, 75, 239, 191, 189, 77, 5, 10, 239, 191, 189, 30, 94, 239, 191, 189, 239, 191, 189, 239, 191, 189, 113, 100, 114, 239, 191, 189, 239, 191, 189, 115, 126, 98, 14, 80, 239, 191, 189, 40, 61, 73, 201, 170, 90, 239, 191, 189, 72, 239, 191, 189, 239, 191, 189, 239, 191, 189, 66, 71, 41, 93, 239, 191, 189, 239, 191, 189, 239, 191, 189, 82, 71, 9, 40, 100, 108, 84, 11, 239, 191, 189, 76, 45, 44, 77, 45, 46, 73, 77, 239, 191, 189, 64, 50, 239, 191, 189, 24, 239, 191, 189, 43, 239, 191, 189, 52, 55, 51, 47, 49, 47, 25, 104, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 82, 74, 102, 113, 9, 68, 210, 184, 54, 239, 191, 189, 22, 0, 88, 239, 191, 189, 18, 239, 191, 189, 239, 191, 189, 0, 0, 0]`,
 right: `[123, 34, 99, 111, 108, 111, 114, 115, 34, 58, 91, 123, 34, 104, 101, 120, 34, 58, 34, 114, 35, 49, 50, 51, 49, 50, 48, 34, 44, 34, 110, 97, 109, 101, 34, 58, 34, 77, 195, 169, 105, 32, 72, 196, 147, 105, 32, 67, 111, 97, 108, 34, 44, 34, 114, 103, 98, 34, 58, 123, 34, 114, 34, 58, 49, 56, 44, 34, 103, 34, 58, 52, 57, 44, 34, 98, 34, 58, 51, 50, 125, 44, 34, 114, 101, 113, 117, 101, 115, 116, 101, 100, 72, 101, 120, 34, 58, 34, 114, 35, 49, 50, 51, 49, 50, 51, 34, 44, 34, 108, 117, 109, 105, 110, 97, 110, 99, 101, 34, 58, 50, 57, 46, 52, 56, 56, 55, 48, 57, 54, 53, 51, 48, 49, 56, 48, 53, 44, 34, 100, 105, 115, 116, 97, 110, 99, 101, 34, 58, 51, 125, 93, 125]`', src/main.rs:84:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
4

1 回答 1

6

https://color.pizza的服务器总是压缩输出,即使客户端没有要求它。

你可以看到这个:

$ curl -i 'https://api.color.pizza/v1/123123' 
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Allow-Credentials: false
Access-Control-Max-Age: 86400
Access-Control-Allow-Headers: X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Wed, 06 May 2020 19:04:47 GMT
Transfer-Encoding: chunked
Via: 1.1 vegur

Warning: Binary output can mess up your terminal. Use "--output -" to tell 
Warning: curl to output it to your terminal anyway, or consider "--output 
Warning: <FILE>" to save to a file.

gzip要解决此问题,您需要启用reqwest

fn main() -> Result<(), Box<dyn std::error::Error>> {
    //Expected output
    let k = r##"{"colors":[{"hex":"#123120","name":"Méi Hēi Coal","rgb":{"r":18,"g":49,"b":32},"requestedHex":"#123123","luminance":29.48870965301805,"distance":3}]}"##;

    let request_url = "https://api.color.pizza/v1/123123";
    let resp = reqwest::blocking::ClientBuilder::new()
        .gzip(true)
        .build()?
        .get(request_url)
        .send()?
        .text()?;

    println!("{}", resp);

    let r1 = resp.into_bytes();
    let r2 = String::from(k).into_bytes();
    assert_eq!(&r1[..], &r2[..]);

    Ok(())
}

这需要以下gzip功能:

[dependencies]
reqwest = { version = "*", features = ["blocking", "gzip"] }
于 2020-05-06T19:18:16.043 回答