Skip to content

How would you download a file? #372

Discussion options

You must be logged in to vote

So downloading a file usually involves making a GET request, so you'd likely use get for that. Then you can pipe the response into a file using the copy_to_file extension method on the response. So an example might look like this:

use isahc::prelude::*;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    // Open an HTTP connection to the server and start streaming the file.
    let mut response = isahc::get("https://example.org/some-file-to-download")?;

    // Check if the server returned a success code.
    if response.status().is_success() {
        // Pipe the response stream to a local file.
        response.copy_to_file("some-file-downloaded")?;
    } else {
        

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by sagebind
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants