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.