Download file from web


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("").expect("request failed");
    let mut out = File::create("").expect("failed to create file");
    io::copy(&mut resp, &mut out).expect("failed to copy content");

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() (

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.


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), 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.