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)?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With