Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get the date when an HTTP file was modified?

I'm trying to check if a file (on web) was modified since the last time I checked. Is it possible to do this by getting http headers to read the last time the file was modified (or uploaded)?

like image 476
Fábio Perez Avatar asked Jul 04 '11 17:07

Fábio Perez


2 Answers

You can use the built-in Net::HTTP library to do most of this for you:

require 'net/http'

Net::HTTP.start('stackoverflow.com') do |http|
  response = http.request_head('/robots.txt')

  response['Last-Modified']
  # => Sat, 04 Jun 2011 08:51:44 GMT
end

If you want, you can convert that to a proper date using Time.parse.

like image 142
tadman Avatar answered Oct 11 '22 20:10

tadman


As @tadman says in his answer, a HTTP "HEAD" request is the proper way to check the last modification date.

You can also do it using a conditional GET request using the "IF-*" modifier headers.

Which to use depends on whether you intend to immediately download the page. If you just want the date use HEAD. If you want the content if there has been a change use GET with the "IF-*" headers.

like image 35
the Tin Man Avatar answered Oct 11 '22 21:10

the Tin Man