Blog

03/06/2019

curl: If-Modified-Since Command Linux / Unix Example



HTTP protocol allows a client to specify a time condition for the document it requests. It is If-Modified-Since or If-Unmodified-Since. How do I use curl Unix/Linux command line option to test a server with If-Modified-Since condition and validate Last-Modified settings?


You can use curl command to see if a copy (http resources such as text/html or image/png) that they hold is still valid. However, this will only work if response has a Last-Modified header. You can send a Last-Modified header using web server or your web application.

Step #1: Find out if response has a Last-Modified header

Type the following curl command:

curl --silent --head http://server1.sxi.io/foo/bar/image.png
curl --silent --head http://server1.sxi.io/foo/help.html

OR

curl -I http://server1.sxi.io/foo/bar/image.png
curl -I  http://server1.sxi.io/foo/help.html

In this example, note down the Last-Modified headers in the response to this HEAD request:
$ curl -I http://sxi.io/
Sample outputs:

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 11 Dec 2012 10:10:24 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Whom: l2-com-cyber
Last-Modified: Tue, 11 Dec 2012 10:10:23 GMT
Cache-Control: max-age=299, must-revalidate
Vary: Cookie
X-Pingback: http://sxi.io/xmlrpc.php
X-Galaxy: Andromeda-1
Vary: Accept-Encoding

The syntax is as follows to send If-Modified-Since header using the curl command line:
$ curl -I --header 'If-Modified-Since: DATE-FORMAT-HERE' http://server1.sxi.io/foo/bar/image.png
$ curl -I --header 'If-Modified-Since: Tue, 11 Dec 2012 10:10:24 GMT' http://sxi.io/

Sample outputs:

HTTP/1.1 304 Not Modified
Server: nginx
Date: Tue, 11 Dec 2012 10:12:11 GMT
Connection: keep-alive
X-Whom: l2-com-cyber
Vary: Cookie
Last-Modified: Tue, 11 Dec 2012 10:10:23 GMT
X-Galaxy: Andromeda-1
Vary: Accept-Encoding

The resource sends a 304 Not Modified response, indicating that it supports Last-Modified validation.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...