rust-cookbook icon indicating copy to clipboard operation
rust-cookbook copied to clipboard

Download example using `response.bytes()` instead of `response.text()`

Open frantufro opened this issue 3 years ago • 0 comments
trafficstars

The current example for downloading files interprets the response as text, and then turns it into bytes.

let content = response.text().await?;
copy(&mut content.as_bytes(), &mut dest)?;

This is fine if you're downloading text files, but it will corrupt binary files (I found this issue when downloading PNGs).

In order to fix this I suggest using response.bytes() directly.

let content = response.bytes().await?;
dest.write(&content)?;

This is how I ended up implementing it and it works great.

frantufro avatar Jul 06 '22 11:07 frantufro