Search code examples
rubyhttpnet-httphttp-head

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?


Solution

  • 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] }