0

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?

Buck Shlegeris
  • 517
  • 6
  • 22
  • 1
    The right way to do a HEAD request with curl is the `--head` option. With `-X HEAD`, curl sends the HEAD method, but still waits for a response body—hence the delays. – Vasiliy Faronov May 27 '16 at 23:58
  • @Vasily--Ah, that makes sense. So the issue is probably that internally Net::HTTP is waiting for a response body and not getting one, right? – Buck Shlegeris May 28 '16 at 00:07

1 Answers1

1

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] }
Chen Pang
  • 1,370
  • 10
  • 11