Bird
Raised Fist0
Rest APIprogramming~10 mins

Last-Modified and If-Modified-Since in Rest API - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the Last-Modified header in the HTTP response.

Rest API
response.headers['Last-Modified'] = [1]
Drag options to blanks, or click blank then click option'
A'If-Modified-Since'
B'Wed, 21 Oct 2015 07:28:00 GMT'
C'200 OK'
D'Content-Type'
Attempts:
3 left
💡 Hint
Common Mistakes
Using header names instead of date strings.
Setting unrelated header values.
2fill in blank
medium

Complete the code to read the If-Modified-Since header from the HTTP request.

Rest API
last_seen = request.headers.get([1])
Drag options to blanks, or click blank then click option'
A'Authorization'
B'Last-Modified'
C'Content-Length'
D'If-Modified-Since'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing with Last-Modified header.
Using unrelated headers.
3fill in blank
hard

Fix the error in the code that compares If-Modified-Since with Last-Modified to decide if the resource is modified.

Rest API
if last_modified <= [1]:
    return 304
Drag options to blanks, or click blank then click option'
Alast_seen
Brequest.headers['If-Modified-Since']
CNone
Dresponse.headers['Last-Modified']
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing header strings directly without parsing.
Using wrong variables.
4fill in blank
hard

Fill both blanks to parse the If-Modified-Since header and compare it with the last modified time.

Rest API
from datetime import datetime

client_time = datetime.strptime([1], '%a, %d %b %Y %H:%M:%S GMT')
if last_modified <= [2]:
    return 304
Drag options to blanks, or click blank then click option'
Alast_seen
Brequest.headers.get('If-Modified-Since')
Cclient_time
Dresponse.headers['Last-Modified']
Attempts:
3 left
💡 Hint
Common Mistakes
Parsing the wrong header.
Comparing string with datetime.
5fill in blank
hard

Fill all three blanks to set Last-Modified header, parse If-Modified-Since, and return 304 if not modified.

Rest API
from datetime import datetime

last_modified = datetime(2023, 6, 1, 12, 0, 0)
response.headers['Last-Modified'] = [1]
client_header = request.headers.get('If-Modified-Since')
client_time = datetime.strptime([2], '%a, %d %b %Y %H:%M:%S GMT') if client_header else None
if client_time and last_modified <= [3]:
    return 304
Drag options to blanks, or click blank then click option'
A'Thu, 01 Jun 2023 12:00:00 GMT'
Bclient_header
Cclient_time
D'Wed, 21 Oct 2015 07:28:00 GMT'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong date strings.
Not checking if client_header exists.
Comparing string with datetime.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To tell the client when the resource was last changed -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    If-Modified-Since: Sun, 01 Jan 2023 12:00:00 GMT -> Option D
  4. 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

  1. 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).
  2. 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.
  3. Final Answer:

    HTTP 200 OK with resource data -> Option C
  4. 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

  1. Step 1: Understand expected server behavior

    The server should compare If-Modified-Since with resource's Last-Modified and respond 304 if not changed.
  2. 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.
  3. Final Answer:

    Server does not check If-Modified-Since header before sending data -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Send Last-Modified header with resource date; on request, check If-Modified-Since and respond 304 if unchanged -> Option B
  4. Quick Check:

    Send Last-Modified + check If-Modified-Since -> 304 optimization [OK]
Hint: Send Last-Modified; respond 304 if If-Modified-Since matches [OK]
Common Mistakes:
  • Sending If-Modified-Since from server instead of client
  • Ignoring If-Modified-Since header in server logic
  • Using Last-Modified only on POST requests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard