I'm trying to fetch an image from Twitter:
open("http://api.twitter.com/1/users/profile_image/barackobama.png?size=bigger")
But I get:
RuntimeError: redirection forbidden: http://... -> https://...
There is an open issue and it seems that I can use an extension to open_uri but I don't know how it works. For example, if I place it in lib/
or if I paste the module in the console, it still doesn't work. Any idea?
I think the proper place to put such a patch is in a file inside config/initializers
, i.e. config/initializers/open_uri_allow_unsafe_redirects_patch.rb
. You have to require 'open-uri'
before reopening the OpenURI
module:
require 'open-uri'
module OpenURI
# the rest of the file here...
end
Then you have to call open
passing the option allow_unsafe_redirects
set to true
:
open('http://api.twitter.com/1/users/profile_image/barackobama.png?size=bigger',
allow_unsafe_redirects: true)
You can find more informations about initializer files on the Ruby on Rails guide