rust-lang-nursery / rust-cookbook

https://rust-lang-nursery.github.io/rust-cookbook
Creative Commons Zero v1.0 Universal
2.27k stars 285 forks source link

web/clients/download/basic doesnt handle binary files #644

Open barrowsys opened 3 years ago

barrowsys commented 3 years ago

https://github.com/rust-lang-nursery/rust-cookbook/blob/master/src/web/clients/download/basic.md doesnt work if trying to download a binary file (such as an image). Might want to either point this out or modify the example to handle all data.

Doesn't work:

    use std::io::copy;
    // ...
    let content =  response.text().await?;
    copy(&mut content.as_bytes(), &mut dest)?;

Does work:

    use std::io::Write;
    // ...
    let content = response.bytes().await()?;
    dest.write_all(&content)?;
stappersg commented 3 years ago

Looks good to me. Asking to @barrowsys to make a merge request. For the fame of being in the git commit log.

barrowsys commented 3 years ago

git is painful but its #645