Markus
Markus

Reputation: 281

How do I download a file over HTTP using Ruby?

How do I download a file over HTTP using Ruby?

Upvotes: 26

Views: 22005

Answers (5)

Clemens Helm
Clemens Helm

Reputation: 3971

Probably the shortest way to download a file:

require 'open-uri'
download = URI.open('http://example.com/')
IO.copy_stream(download, './my_file.html')

Upvotes: 28

KrauseFx
KrauseFx

Reputation: 11751

You can use open-uri, which is a one liner

require 'open-uri'

content = open('http://example.com').read

Upvotes: 12

MattMcKnight
MattMcKnight

Reputation: 8290

require 'net/http'
#part of base library
Net::HTTP.start("your.webhost.com") { |http|
  resp = http.get("/yourfile.xml")
  open("yourfile.xml", "wb") { |file|
    file.write(resp.body)
  }
}

Upvotes: 14

JRL
JRL

Reputation: 77995

Simple...

response = Net::HTTP.get_response(URI.parse("yourURI"))

Upvotes: 7

Jordan Running
Jordan Running

Reputation: 106027

There are several ways, but the easiest is probably OpenURI. This blog post has some sample code, and also goes over Net::HTTP (with Hpricot) and Rio.

Upvotes: 5

Related Questions