Open-uri without ssl / https verification

Usage

require 'open-uri'
require 'openssl'
OpenURI.without_ssl_verification do
  open('https://foo').read
end

Code

module OpenURI
  def self.without_ssl_verification
    old = ::OpenSSL::SSL::VERIFY_PEER
    silence_warnings{ ::OpenSSL::SSL.const_set :VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE }
    yield
  ensure
    silence_warnings{ ::OpenSSL::SSL.const_set :VERIFY_PEER, old }
  end
end

If you are not in Rails you may need silence_warnings

One thought on “Open-uri without ssl / https verification

Leave a comment