Status
rowing on the Ganges
Location
Varanasi, India
Subscribe to GeoRSS Subscribe to KML


open-uri and can’t convert Hash into String

Published in Programming, Ruby  |  6 Comments


I’m posting this in hopes that search engines grab it and put it at the top of their list when other poor soul’s run into this problem.

If you’re trying to grab a web resource using Open-URI, and you are using basic authentication (username/password) then you’ll need to make sure to require 'open-uri' or you’ll get:

open("http://example.com/site", :http_basic_authentication => ["username", "password"])
TypeError: can't convert Hash into String

Of course, then you toss some yummy Hpricot into the mix for parsing/scraping the good bits of the HTML.

Similar Posts


Responses

  1. Peter says:

    March 20th, 2007 at 9:36 am (#)

    Or you can use something like scRUBYt! – basic authentication is not yet supported in the present version, but will be added with the next release.

  2. Andy Watts says:

    October 23rd, 2008 at 3:07 pm (#)

    Googled and got your post.
    /doh. I forgot to require ‘open-uri’
    Thankyou.

  3. rob says:

    September 9th, 2009 at 5:35 am (#)

    DUDE!

    Thank god I googled this issue…such a rookie mistake…I can’t believe I wasted 30 minutes trying to debug otherwise perfect code.

    Thanks so much!

  4. When open-uri can’t convert Hash into String — another time it happens « catapult-creative.com says:

    October 25th, 2009 at 8:20 pm (#)

    [...] It’s not calling the part you might think — the piece where it asks if the name can be converted to a string and if it conforms to a loose URI regex pattern. It’s instead calling it with the original, version of open, the one that the Kernel class provides so you can easily open files and URLs (but without all the tasty options given you by OpenURI). This error gets thrown unhelpfully by Kernel when you try to use open outside the context of OpenURI (as this guy points out). [...]

  5. Pat says:

    October 29th, 2009 at 10:03 am (#)

    Thank you! I ran into a very similar problem (can’t convert URI::HTTP into String) b/c I’d forgotten to require open-uri. /doh indeed.

  6. Anders Carlsen says:

    February 16th, 2010 at 11:24 am (#)

    obvius
    thanks :-)

Leave a Response