Logo

Programming-Idioms

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

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.

require 'net/http'
u = URI('http://example.com/large_file')

Net::HTTP.start(u.host, u.port) do |http|
  request = Net::HTTP::Get.new(u)
  http.request(request) do |response|
    open('result.txt', 'w') do |file|
      response.read_body do |chunk|
        file.write(chunk)
      end
    end
  end
end
require 'open-uri'
response = URI.open(u).read
File.write("result.txt", response)
import std.net.curl;
download(u, "result.txt");
import "fmt"
import "io"
import "net/http"
out, err := os.Create("result.txt")
if err != nil {
	return err
}
defer out.Close()

resp, err := http.Get(u)
if err != nil {
	return err
}
defer func() {
	io.Copy(io.Discard, resp.Body)
	resp.Body.Close()
}()
if resp.StatusCode != 200 {
	return fmt.Errorf("Status: %v", resp.Status)
}

_, err = io.Copy(out, resp.Body)
if err != nil {
	return err
}
(ql:quickload '("serapeum" "dexador"))
(serapeum:write-stream-into-file
   (dex:get u :want-stream t)
              #P"result.txt"
              :direction :output
              :if-exists :supersede
              :if-does-not-exist :create)
uses fphttpclient, classes;
var
  str: TMemorystream;
begin
  with TFPHTTPClient.create(nil) do 
  try
    str := TMemoryStream.create;
    try
      get(u, str);
      str.saveToFile('result.txt');
    finally
      str.free;
    end;
  finally
    free;
  end;
end;
use HTTP::Tiny qw();
my $response = HTTP::Tiny->new->mirror($u, 'result.txt');
import urllib
filename, headers = urllib.request.urlretrieve(u, 'result.txt')
import requests
with open("results.txt", "wb") as fh:
	fh.write(requests.get(u).content)
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),
};

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