0
0
Rest APIprogramming~30 mins

Last-Modified and If-Modified-Since in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(response) to show the result.