Dave Dave - 4 months ago 9
Ruby Question

How can I get both header and web page content in a single call?

I’m using Rails 4.2.7. I know how to use openURI to get the headers from a URL …

open(url){|f| pp f.meta }


and I know how to get the contents of the URL

open(url).read


So how can I get both headers and contents in one call, preferably storing headers into one variable and contents into another?

Answer

You just have to reuse the result of the open call:

f = open(url)
pp f.meta
pp f.read