public
Created

GET a content of a https URL (handling redirects)

  • Download Gist
get_https_uri.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
require "uri"
require "net/https"
 
location = "https://..." # the URL to GET
 
begin
uri = URI.parse(location)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
case response = http.request(request)
when Net::HTTPRedirection then location = response['location']
when Net::HTTPClientError, Net::HTTPServerError then response.error!
end
end until Net::HTTPSuccess === response
 
puts response.body

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.