Download file from web

Hey,

I don’t find how to download file from a website. I have find reqwest but it’s don’t work and i don’t not if it’s possible with curl.

This works fine.

extern crate reqwest;

use std::io;
use std::fs::File;

fn main() {
    let mut resp = reqwest::get("https://sh.rustup.rs").expect("request failed");
    let mut out = File::create("rustup-init.sh").expect("failed to create file");
    io::copy(&mut resp, &mut out).expect("failed to copy content");
}
8 Likes

Thanks you!!! :smiley:

I've tried that but the trait std::io::Read is not implemented for reqwest::async_impl::response::Response.

So I went to the API documentation and found I either can use bytes() or text() (https://docs.rs/reqwest/0.10.4/reqwest/struct.Response.html#method.bytes).

I used text first and the file write was erroneous. I downloaded a PNG file but it wasn't recognized.

So now I use .bytes().await.unwrap() and now the compiler says the trait std::io::Read is not implemented for bytes::bytes::Bytes.

This annoys me very much.

This thread is two years old and was written for a different version of reqwest.

You can use .as_ref() to convert from Bytes to &[u8], which implements Read.

4 Likes

no method named as_ref found for struct reqwest::async_impl::response::Response in the current scope
method not found in reqwest::async_impl::response::Responserustc(E0599)
mod.rs(167, 8): the method is available for std::boxed::Box<reqwest::async_impl::response::Response> here

Downloading a file from the internet is getting out of hand :sweat_smile:

You still need to call response.bytes() to get the response body.

let bytes = response.bytes();
let slice: &[u8] = bytes.as_ref();
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.