Using Last-Modified and If-Modified-Since Headers in a REST API
📖 Scenario: You are building a simple REST API that serves information about books. To optimize network usage, you want to use HTTP headers Last-Modified and If-Modified-Since so clients only download data when it has changed.
🎯 Goal: Build a REST API endpoint that returns book data with a Last-Modified header. The API should check the client's If-Modified-Since header and respond with 304 Not Modified if the data has not changed since that time.
📋 What You'll Learn
Create a data structure holding book information and a last modified timestamp
Add a variable holding the last modified time of the data
Write logic to check the If-Modified-Since header from the client
Return 304 Not Modified if data is unchanged, otherwise return book data with Last-Modified header
💡 Why This Matters
🌍 Real World
APIs use Last-Modified and If-Modified-Since headers to reduce data transfer and improve performance by sending data only when it changes.
💼 Career
Understanding HTTP caching headers is important for backend developers and API designers to build efficient and scalable web services.
Progress0 / 4 steps
1
DATA SETUP: Create book data and last modified timestamp
Create a dictionary called books with these exact entries: "id": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald". Also create a variable called last_modified and set it to the string "Wed, 21 Oct 2015 07:28:00 GMT".
Rest API
Hint
Use a dictionary for books with the exact keys and values. Set last_modified as a string with the exact date.
2
CONFIGURATION: Simulate getting the If-Modified-Since header
Create a variable called if_modified_since and set it to the string "Wed, 21 Oct 2015 07:28:00 GMT" to simulate the client's If-Modified-Since header.
Rest API
Hint
Set if_modified_since exactly as the string shown to simulate the header.
3
CORE LOGIC: Check If-Modified-Since and decide response
Write an if statement that compares if_modified_since and last_modified. If they are equal, set a variable response to the string "304 Not Modified". Otherwise, set response to a dictionary with keys "data" (value is books) and "Last-Modified" (value is last_modified).
Rest API
Hint
Use an if to compare the two strings exactly. Assign response accordingly.
4
OUTPUT: Print the response
Write a print statement to display the response variable.
Rest API
Hint
Use print(response) to show the result.
Practice
(1/5)
1. What is the main purpose of the Last-Modified header in a REST API response?
easy
A. To tell the client when the resource was last changed
B. To tell the client the size of the resource
C. To tell the client the type of the resource
D. To tell the client the server's current time
Solution
Step 1: Understand the role of Last-Modified header
The Last-Modified header indicates the date and time when the resource was last changed on the server.
Step 2: Compare options with the header's purpose
Only To tell the client when the resource was last changed correctly describes this purpose. Other options describe unrelated information.
Final Answer:
To tell the client when the resource was last changed -> Option A
Quick Check:
Last-Modified = resource last change time [OK]
Hint: Last-Modified shows last change date of resource [OK]
Common Mistakes:
Confusing Last-Modified with Content-Length
Thinking it shows server current time
Mixing it up with Content-Type header
2. Which of the following is the correct syntax to send an If-Modified-Since header in an HTTP request?
easy
A. If-Modified-Since: 2023-01-01T12:00:00Z
B. If-Modified-Since = "2023-01-01 12:00:00"
C. If-Modified-Since -> 01/01/2023 12:00
D. If-Modified-Since: Sun, 01 Jan 2023 12:00:00 GMT
Solution
Step 1: Recall HTTP header date format
HTTP headers use the RFC 1123 date format like Sun, 01 Jan 2023 12:00:00 GMT for date/time values.
Step 2: Match options to correct format
If-Modified-Since: Sun, 01 Jan 2023 12:00:00 GMT matches the correct header syntax and date format. Other options use invalid syntax or date formats.
Final Answer:
If-Modified-Since: Sun, 01 Jan 2023 12:00:00 GMT -> Option D
Quick Check:
If-Modified-Since uses RFC 1123 date format [OK]
Hint: Use RFC 1123 date format for If-Modified-Since header [OK]
Common Mistakes:
Using ISO 8601 instead of RFC 1123 format
Using '=' instead of ':' in header
Using arrow '->' instead of colon
3. Given the server responds with header Last-Modified: Wed, 10 May 2023 10:00:00 GMT, what will the server respond if the client sends If-Modified-Since: Wed, 10 May 2023 09:00:00 GMT?
medium
A. HTTP 400 Bad Request
B. HTTP 304 Not Modified
C. HTTP 200 OK with resource data
D. HTTP 500 Internal Server Error
Solution
Step 1: Compare If-Modified-Since with Last-Modified
The client's If-Modified-Since date is earlier (09:00) than the server's Last-Modified date (10:00).
Step 2: Determine server response based on dates
Since the resource was modified after the client's date, the server sends full data with HTTP 200 OK.
Final Answer:
HTTP 200 OK with resource data -> Option C
Quick Check:
If-Modified-Since < Last-Modified -> 200 OK [OK]
Hint: If client date before last change, server sends full data [OK]
Common Mistakes:
Thinking server sends 304 when resource changed after client date
Confusing 400 Bad Request with conditional headers
Assuming server error on date mismatch
4. A client sends If-Modified-Since: Tue, 09 May 2023 10:00:00 GMT but the server always responds with HTTP 200 OK and full data, ignoring the header. What is the likely error in server handling?
medium
A. Server does not check If-Modified-Since header before sending data
B. Server sends wrong Last-Modified date format
C. Client sent If-Modified-Since header incorrectly
D. Server is missing Content-Type header
Solution
Step 1: Understand expected server behavior
The server should compare If-Modified-Since with resource's Last-Modified and respond 304 if not changed.
Step 2: Identify why server always sends 200 OK
If server ignores the header and always sends full data, it likely does not check the If-Modified-Since header properly.
Final Answer:
Server does not check If-Modified-Since header before sending data -> Option A
Quick Check:
Ignoring If-Modified-Since means always 200 OK [OK]
Hint: Server must check If-Modified-Since to send 304 [OK]
Common Mistakes:
Blaming client for server ignoring header
Thinking wrong Last-Modified format causes 200 OK
Confusing Content-Type header with caching headers
5. You want to optimize your REST API to reduce bandwidth by using Last-Modified and If-Modified-Since. Which approach correctly implements this optimization?
hard
A. Ignore If-Modified-Since and always send full resource with Last-Modified
B. Send Last-Modified header with resource date; on request, check If-Modified-Since and respond 304 if unchanged
C. Send If-Modified-Since header from server to client to indicate last change
D. Use Last-Modified header only on POST requests
Solution
Step 1: Understand how Last-Modified and If-Modified-Since work together
The server sends Last-Modified header with the resource's last change date. The client sends If-Modified-Since header on next request to ask if resource changed since that date.
Step 2: Implement correct server logic for optimization
The server compares If-Modified-Since with resource's last modified date and returns HTTP 304 Not Modified if unchanged, saving bandwidth.
Final Answer:
Send Last-Modified header with resource date; on request, check If-Modified-Since and respond 304 if unchanged -> Option B