Get File Creation Date Over HTTP
Asked Answered
A

3

12

Given a file on a webserver (e.g., http://foo.com/bar.zip -> only accessible through HTTP), is there any way to get the date attributes (e.g., date [created, modified]) without downloading the entire archive in the first place?

Right now, I download the archive and read the attributes programmatically. Trouble is that the archive is dozens of MiB so it seems like a waste of resources to download the entire thing and end up reading off just a couple of bytes of information.

I realize that bandwidth is practically free, but I don't like to be wasteful in any case.

Amero answered 1/12, 2010 at 16:35 Comment(2)
How exactly do you determine the file modification dates? By analyzing the stored file? Or do you read the HTTP headers?Chilli
@kork: right now I download the archive, open it up (Perl's Archive::Zip) and get the date information that way. I was hoping to be able to request this information via HTTP (the archive creation date alone will do) from the webserver's file system.Amero
K
11

Try to read Last-Modified from header

Karee answered 1/12, 2010 at 16:40 Comment(2)
Thanks ihorko, I'll take a look and report back.Amero
Looking at a header trace, it looks like all the information that I need is provided. I can't believe I didn't bother to look at the headers first. :) Thanks!Amero
E
9

Be sure to use a HTTP HEAD request instead of a HTTP GET request to read the HTTP headers only. If you do a HTTP GET, you will download the whole file nevertheless, even if you decide just to inspect the HTTP headers.

Eckardt answered 1/12, 2010 at 17:9 Comment(0)
F
5

Just for the sake of simplicity, here's a compilation of the existing (perfect) answers from @ihorko and @JanThomä, that uses curl. Other option are available too, of course, but here's a fully functional answer.

Use curl with the -I option:

-I, --head
(HTTP/FTP/FILE) Fetch the HTTP-header only! HTTP-servers feature the command HEAD which this uses to get nothing but the header of a document. When used on an FTP or FILE file, curl displays the file size and last modification time only.

Also, the -s option is nice here:

-s, --silent
Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. It will still output the data you ask for, potentially even to the terminal/stdout unless you redirect it.

Hence, something like this would do the trick:

curl -sI http://foo.com/bar.zip | grep 'Last-Modified' | cut -d' ' -f 2-
Furculum answered 8/9, 2014 at 12:4 Comment(1)
Thanks for this functional answer. I would add the -i (case-insensitive) option to the grep command. Also with the windows build of grep, single quotes seem to be interpreted literally, better to use double quotes, or no quotes since there are no spaces. I used: curl -sI <file-address> | grep -i "Last-Modified"Translate

© 2022 - 2024 — McMap. All rights reserved.