2

How do I read the header only in Ruby? I tried some of the options in the documentation for open but it still reads the entire file.

http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI/OpenRead.html#method-i-open

irb(main):153:0> html=open(url, :content_length_proc => lambda {|len| p len  })
177602
=> #<Tempfile:C:/Users/Chloe/AppData/Local/Temp/open-uri20140220-8496-o5zlda>

I want something like curl -I or wget --spider.


Ok found two ways!

open(url){|f| f.meta['content-length']}

Net::HTTP.start(uri.host) {|http| http.head(uri.path)['content-length']}

I like the first as it's simpler and doesn't require creating then parsing a URI object.

Chloe
  • 25,162
  • 40
  • 190
  • 357

2 Answers2

3

As far as I'm aware this isn't possible with OpenURI but is with Net:HTTP (this may have changed). See this answer:

Get HTTP Headers before downloading with open-uri (Ruby)

which suggests you use Net::HTTP's request_get method.

Richard Jordan
  • 8,066
  • 3
  • 39
  • 45
1

If you're open to trying a gem, httpclient is great for that:

require 'httpclient'
HTTPClient.new.head('http://google.com')
Abdo
  • 13,549
  • 10
  • 79
  • 98
  • I think the solution you suggested would execute a HEAD request and not retrieve the headers of the request. To retrieve the header with httpclient, you should do something like `HTTPClient.new.get('http://google.com').headers`. However, this would require to read the all response. – Raphael Pr Jan 02 '20 at 15:05