How would you download a file? #372
Answered
by
sagebind
suryavirkapur
asked this question in
Questions
-
Hi guys, I am new to Rust and the crate itself. I would really appreciate if you could give me a hint into how I could download a file using isahc. |
Beta Was this translation helpful? Give feedback.
Answered by
sagebind
Feb 2, 2022
Replies: 2 comments
-
So downloading a file usually involves making a 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 {
println!("Server returned status code {}", response.status());
}
Ok(())
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sagebind
-
Thank you. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So downloading a file usually involves making a
GET
request, so you'd likely useget
for that. Then you can pipe the response into a file using thecopy_to_file
extension method on the response. So an example might look like this: