Buck Shlegeris Buck Shlegeris - 6 months ago 29
Ruby Question

Net::HTTP failing on a head request

I'm trying to make an HTTP Head request using Net::HTTP.

require 'net/http'

uri = URI("https://github.com/rails/rails")
http = Net::HTTP.new(uri.host, uri.port)
request = http.head(uri)

puts request


fails.

AFAICT, this is because Net::HTTP is waiting on a response body which will never come. How do I ask Net::HTTP to make a request and not wait on the response body?

Answer

If you follow the documentation properly, it works just fine. The library implementation probably has some assumptions on the usage when it determines whether to read the payload.

response = nil
Net::HTTP.start('github.com', :use_ssl => true) do |http|
  response = http.head('/rails/rails')
end

response.each { |k, v| [k, v] }