Ruby: Downloading files from the Internet

This is easy:

require 'open-uri'

File.open('/target/path/to/downloaded.file', "wb") do |file|
  file.write open('http://example.com/your.file').read
end

Basic Authentication

When your file is protected by HTTP Basic Authentication, pass your credentials as hash:

File.open('/target/path/to/downloaded.file', "wb") do |file|
  file.write open('http://example.com/your.file', :http_basic_authentication => [your_username, your_password]).read
end
Dominik Schöler Over 12 years ago