Type:
Module
Constants:
Options : { :proxy => true, :proxy_http_basic_authentication => true, :progress_proc => true, :content_length_proc => true, :http_basic_authentication => true, :read_timeout => true, :ssl_ca_cert => nil, :ssl_verify_mode => nil, :ftp_active_mode => false, :redirect => true, }

OpenURI is an easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP.

Example

It is possible to open an http, https or ftp URL as though it were a file:

1
2
3
  f.each_line {|line| p line}
}

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

1
2
3
4
5
6
7
8
  f.each_line {|line| p line}
  p f.base_uri         # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/>
  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.

1
2
3
4
5
6
  "User-Agent" => "Ruby/#{RUBY_VERSION}",
  "From" => "foo@bar.invalid",
  "Referer" => "http://www.ruby-lang.org/") {|f|
  # ...
}

The environment variables such as http_proxy, https_proxy and ftp_proxy are in effect by default. Here we disable proxy:

1
2
3
open("http://www.ruby-lang.org/en/", :proxy => nil) {|f|
  # ...
}

See OpenURI::OpenRead#open and Kernel#open for more on available options.

URI objects can be opened in a similar way.

1
2
3
4
uri = URI.parse("http://www.ruby-lang.org/en/")
uri.open {|f|
  # ...
}

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

1
2
str = uri.read
p str.base_uri
Author

Tanaka Akira <akr@m17n.org>

charset
  • References/Ruby on Rails/Ruby/Classes/OpenURI/OpenURI::Meta

charset() Instance Public methods returns a charset parameter in Content-Type

2025-01-10 15:47:30
last_modified
  • References/Ruby on Rails/Ruby/Classes/OpenURI/OpenURI::Meta

last_modified() Instance Public methods returns a

2025-01-10 15:47:30
content_encoding
  • References/Ruby on Rails/Ruby/Classes/OpenURI/OpenURI::Meta

content_encoding() Instance Public methods Returns a list of encodings in Content-Encoding

2025-01-10 15:47:30
new
  • References/Ruby on Rails/Ruby/Classes/OpenURI/OpenURI::HTTPRedirect

new(message, io, uri) Class Public methods

2025-01-10 15:47:30
new
  • References/Ruby on Rails/Ruby/Classes/OpenURI/OpenURI::HTTPError

new(message, io) Class Public methods

2025-01-10 15:47:30
open
  • References/Ruby on Rails/Ruby/Classes/OpenURI/OpenURI::OpenRead

open(*rest, &block) Instance Public methods

2025-01-10 15:47:30
read
  • References/Ruby on Rails/Ruby/Classes/OpenURI/OpenURI::OpenRead

read(options={}) Instance Public methods

2025-01-10 15:47:30
content_type
  • References/Ruby on Rails/Ruby/Classes/OpenURI/OpenURI::Meta

content_type() Instance Public methods returns âtype/subtypeâ which is MIME

2025-01-10 15:47:30