In Files

  • open-uri.rb


OpenURI is an easy-to-use wrapper for net/http, net/https and net/ftp.


It is possible to open http/https/ftp URL as usual like opening a file:

  open("") {|f|
    f.each_line {|line| p line}

The opened file has several methods for meta information as follows since
it is extended by OpenURI::Meta.

  open("") {|f|
    f.each_line {|line| p line}
    p f.base_uri         # <URI::HTTP:0x40e6ef2 URL:>
    p f.content_type     # "text/html"
    p f.charset          # "iso-8859-1"
    p f.content_encoding # []
    p f.last_modified    # Thu Dec 05 02:45:02 UTC 2002

Additional header fields can be specified by an optional hash argument.

    "User-Agent" => "Ruby/#{RUBY_VERSION}",
    "From" => "foo@bar.invalid",
    "Referer" => "") {|f|
    # ...

The environment variables such as http_proxy, https_proxy and ftp_proxy
are in effect by default.  :proxy => nil disables proxy.

  open("", :proxy => nil) {|f|
    # ...

URI objects can be opened in a similar way.

  uri = URI.parse("") {|f|
    # ...

URI objects can be read directly. The returned string is also extended by

  str =
  p str.base_uri

Author:: Tanaka Akira <>



Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit