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

I am trying to check if a file (on the Internet) has been modified since the last check. Can this be done by getting the HTTP headers to read the last time the file was modified (or downloaded)?

+6
source share
2 answers

You can use the Net :: HTTP built-in 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 it to the desired date using Time.parse .

+10
source

As @tadman says in his answer, an HTTP "HEAD" request is the right way to check the date of the last change.

You can also do this by using a GET conditional query using IF- * modifier headers.

What you need to use depends on whether you intend to immediately load the page. If you just want the date to use HEAD. If you want the content, if changes have occurred, use a GET with the headers "IF- *".

+3
source

Source: https://habr.com/ru/post/891974/


All Articles