2

I am trying to decompress a gzipped response. reqwest has a client builder flag that enables decompressing with features flag gzip. I am using async client with tokio runtime.

//Cargo.toml
reqwest = { version = "0.11.8", features = ["gzip"] }
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = reqwest::ClientBuilder::new()
        .gzip(true)
        .build()?;
    let b = client
        .get("https://wiki.mozilla.org/images/f/ff/Example.json.gz")
        .send()
        .await?
        .text()
        .await?;
        
    println!("{}", b);
    Ok(())
}
kmdreko
  • 42,554
  • 6
  • 57
  • 106
katrocitus
  • 45
  • 1
  • 6
  • 7
    I don't think that's what the gzip option does, it does not decompress a file, but it decompresses the data stream if it was marked as encoded in gzip (marked in the response header `Content-Encoding`). I think you can just use a gzip crate to decompress it. – 8176135 Jan 11 '22 at 06:01
  • I can confirm what @8176135 said, the purpose of stream decompression is to decompress [the HTTP stream](https://en.wikipedia.org/wiki/HTTP_compression), not to decompress a file sent over that stream. – Masklinn Jan 11 '22 at 06:58
  • 1
    that clears it up. I have to manually decode it then. – katrocitus Jan 11 '22 at 07:09
  • 1
    In other words, `gzip()` enables transparent decompression of stream-compressed data. In this case the file you're being sent is just a gzip file, and it's not _supposed_ to be automatically decompressed. Indeed, if you run `curl -v https://wiki.mozilla.org/images/f/ff/Example.json.gz > x`, you'll see that the servre doesn't sends a `Content-Type: application/x-gzip`, but not a `Content-Encoding: gzip`, which is why curl just saved the file in the compressed form received over the wire. – user4815162342 Jan 11 '22 at 08:11

0 Answers0