0
0
Rest APIprogramming~5 mins

Last-Modified and If-Modified-Since in Rest API

Choose your learning style9 modes available
Introduction

These headers help servers and clients save time and data by checking if content has changed before sending it again.

When you want to avoid sending the same webpage or data if it hasn't changed since the last visit.
When building APIs that return data which updates occasionally, like news or weather.
When you want to reduce bandwidth usage by not sending unchanged files.
When improving website speed by letting browsers cache content smartly.
When you want to tell clients the last time your resource was updated.
Syntax
Rest API
Last-Modified: <HTTP-date>
If-Modified-Since: <HTTP-date>

Last-Modified is sent by the server to tell when the resource was last changed.

If-Modified-Since is sent by the client to ask the server to send data only if it changed after this date.

Examples
Server tells client the resource was last changed on this date.
Rest API
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
Client asks server to send data only if it changed after this date.
Rest API
If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT
Server replies with 304 status if resource has not changed since the date client sent.
Rest API
HTTP/1.1 304 Not Modified

Sample Program

This simple server sends a fixed last modified date. If the client sends the same date in If-Modified-Since, it replies with 304 Not Modified. Otherwise, it sends the content with the Last-Modified header.

Rest API
from http.server import BaseHTTPRequestHandler, HTTPServer
from datetime import datetime

class SimpleHandler(BaseHTTPRequestHandler):
    last_modified = datetime(2024, 6, 1, 12, 0, 0)

    def do_GET(self):
        # Format last_modified as HTTP date
        last_mod_str = self.last_modified.strftime('%a, %d %b %Y %H:%M:%S GMT')

        # Get If-Modified-Since header from client
        ims = self.headers.get('If-Modified-Since')

        if ims == last_mod_str:
            # Resource not modified
            self.send_response(304)
            self.end_headers()
            return

        # Resource modified or no If-Modified-Since header
        self.send_response(200)
        self.send_header('Content-type', 'text/plain')
        self.send_header('Last-Modified', last_mod_str)
        self.end_headers()
        self.wfile.write(b'Hello! This is the latest content.')

if __name__ == '__main__':
    server_address = ('', 8000)
    httpd = HTTPServer(server_address, SimpleHandler)
    print('Server running on http://localhost:8000')
    httpd.serve_forever()
OutputSuccess
Important Notes

The date format must follow the HTTP-date format like: 'Wed, 21 Oct 2015 07:28:00 GMT'.

Clients use If-Modified-Since to avoid downloading unchanged data, saving bandwidth.

Servers respond with 304 status to tell clients to use cached data.

Summary

Last-Modified tells when data was last changed.

If-Modified-Since asks if data changed since a date.

These headers help save time and data by avoiding unnecessary downloads.