OpenURI is a Ruby module included in the standard library, which provides an easy-to-use wrapper for net/http, net/https and net/ftp.
Questions tagged [open-uri]
311 questions
41
votes
1 answer
How to specify "http request header" in OpenURI
I am trying to call a URL using Ruby's OpenURI gem, however it needs me to pass certain values inside its HTTP request header.
Any idea how to do this?

iwan
- 7,269
- 18
- 48
- 66
36
votes
4 answers
Ruby open-uri redirect forbidden
I have this simple html parser(for learning purposes) that I have been working on.:
require 'open-uri'
puts "Enter URL to parse HTML: "
url = gets.chomp
puts "Enter tag to parse from: "
tag = gets.chomp
response = open(url).read
title1 =…

Vikaton
- 2,227
- 3
- 14
- 23
35
votes
9 answers
Is there a workaround to open URLs containing underscores in Ruby?
I'm using open-uri to open URLs.
resp = open("http://sub_domain.domain.com")
If it contains underscore I get an error:
URI::InvalidURIError: the scheme http does not accept registry part: sub_domain.domain.com (or bad hostname?)
I understand…

Arty
- 5,923
- 9
- 39
- 44
34
votes
3 answers
Why does Ruby open-uri's open return a StringIO in my unit test, but a FileIO in my controller?
I inherited a Rails 2.2.2 app that stores user-uploaded images on Amazon S3. The attachment_fu-based Photo model offers a rotate method that uses open-uri to retrieve the image from S3 and MiniMagick to perform the rotation.
The rotate method…

Sean Santry
- 343
- 1
- 3
- 4
33
votes
8 answers
Retrieve contents of URL as string
For tedious reasons to do with Hpricot, I need to write a function that is passed a URL, and returns the whole contents of the page as a single string.
I'm close. I know I need to use OpenURI, and it should look something like this:
require…

AP257
- 89,519
- 86
- 202
- 261
31
votes
4 answers
How do I make a POST request with open-uri?
Is it possible to make a POST request from Ruby with open-uri?

Alexey Lebedev
- 11,988
- 4
- 39
- 47
29
votes
1 answer
How can I handle 503 errors with open-uri?
If a website returns a '503 service unavailable' error then open-uri throws an exception. For example:
require 'open-uri'
open('http://www.google.co.uk/sorry/?continue=http://www.google.co.uk/search%3Fq%3Dhello%26oq%3Dhello%26ie%3DUTF-8')
#…

Max Rose-Collins
- 1,904
- 5
- 26
- 45
27
votes
8 answers
Nokogiri, open-uri, and Unicode Characters
I'm using Nokogiri and open-uri to grab the contents of the title tag on a webpage, but am having trouble with accented characters. What's the best way to deal with these? Here's what I'm doing:
require 'open-uri'
require 'nokogiri'
doc =…

Moe
- 641
- 1
- 7
- 16
24
votes
2 answers
What is the difference between Ruby's 'open-uri' and 'Net:HTTP' gems?
It seems like both of these gems perform very similar tasks. Can anyone give examples of where one gem would be more useful than the other? I don't have specific code that I'm referring to, I'm more wondering about general use cases for each gem. …

Richie Thomas
- 3,073
- 4
- 32
- 55
24
votes
6 answers
Ruby's open-uri and cookies
I would like to store the cookies from one open-uri call and pass them to the next one. I can't seem to find the right docs for doing this. I'd appreciate it if you could tell me the right way to do this.
NOTES: w3.org is not the actual url, but…

dlamblin
- 43,965
- 20
- 101
- 140
22
votes
1 answer
Fast way to get remote image dimensions
I'm using the imagesize gem to check the sizes of remote images and then only push images that are big enough into an array.
require 'open-uri'
require 'image_size'
data = Nokogiri::HTML(open(url))
images = []
forcenocache = Time.now.to_i # No cache…

Aen Tan
- 3,305
- 6
- 32
- 52
21
votes
2 answers
Rails - How to send an image from a controller
in my rails app, I need to pass back a image.
I have a 1x1.gif tracking pixel in my route as follows:
match "/_ctrack.gif" => "email_tracking_pixels#my_method"
In the controller:
def my_method
send_data…

AnApprentice
- 108,152
- 195
- 629
- 1,012
19
votes
4 answers
In Ruby/Rails, how can I encode/escape special characters in URLs?
How do I encode or 'escape' the URL before I use OpenURI to open(url)?
We're using OpenURI to open a remote url and return the xml:
getresult = open(url).read
The problem is the URL contains some user-input text that contains spaces and other…

jpw
- 18,697
- 25
- 111
- 187
19
votes
2 answers
"No such file or directory @ rb_sysopen" using OpenURI
I've recently run into a problem using OpenURI. Every open method results in the following error:
"No such file or directory @ rb_sysopen".
My code looks simply like the following:
data = open("http://google.ca/")
I noticed the error shortly after…

Jamie
- 1,607
- 3
- 17
- 25
18
votes
1 answer
Ruby open-uri can't open url (m1 mac)
i start to learn ruby and scraping and i try to open an url with open and i got
lib/scrapper.rb:7:in `initialize': No such file or directory @ rb_sysopen - https://en.wikipedia.org/wiki/Douglas_Adams (Errno::ENOENT) from lib/scrapper.rb:7:in `open'…

Maxime Crespo
- 199
- 1
- 10