Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Rust

Idiom #102 Load from HTTP GET request into a file

Make an HTTP request with method GET to the URL u, then store the body of the response in the file result.txt. Try to save the data as it arrives if possible, without having all its content in memory at once.

extern crate reqwest;
use reqwest::Client;
use std::fs::File;
let client = Client::new();
match client.get(&u).send() {
    Ok(res) => {
        let file = File::create("result.txt")?;
        ::std::io::copy(res, file)?;
    },
    Err(e) => eprintln!("failed to send request: {}", e),
};
import std.net.curl;
download(u, "result.txt");

New implementation...
< >
programming-idioms.org