0
0
Rest APIprogramming~20 mins

Last-Modified and If-Modified-Since in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Last-Modified Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the HTTP status code returned?

Consider a REST API endpoint that returns a resource with the header Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT. A client sends a GET request with the header If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT. What status code will the server most likely return?

A200 OK - The resource is returned with full content.
B304 Not Modified - The resource has not changed since the given date.
C404 Not Found - The resource does not exist.
D500 Internal Server Error - Server failed to process the request.
Attempts:
2 left
💡 Hint

Think about what the If-Modified-Since header asks the server to do.

🧠 Conceptual
intermediate
1:30remaining
Purpose of Last-Modified header

What is the main purpose of the Last-Modified HTTP header in REST APIs?

ATo tell the client when the resource was last changed, enabling conditional requests.
BTo specify the expiration time of the resource in the cache.
CTo provide the server's current time.
DTo indicate the size of the resource in bytes.
Attempts:
2 left
💡 Hint

Think about how clients can avoid downloading unchanged data.

Predict Output
advanced
2:00remaining
What happens if If-Modified-Since is after Last-Modified?

A server resource has Last-Modified: Mon, 01 Jan 2024 10:00:00 GMT. A client sends a GET request with If-Modified-Since: Tue, 02 Jan 2024 10:00:00 GMT. What will the server respond?

A400 Bad Request due to invalid date.
B200 OK with the full resource content.
C304 Not Modified with no body.
D503 Service Unavailable.
Attempts:
2 left
💡 Hint

Consider if the resource has changed since the date the client provides.

🔧 Debug
advanced
2:30remaining
Why does the server always return 200 OK?

A developer implements a REST API that sets Last-Modified header correctly. However, clients always receive 200 OK responses even when sending If-Modified-Since headers. What is the most likely cause?

AThe client is sending the wrong HTTP method instead of GET.
BThe <code>Last-Modified</code> header is set to a future date.
CThe server is missing the <code>Content-Type</code> header.
DThe server does not check the <code>If-Modified-Since</code> header and always returns the full resource.
Attempts:
2 left
💡 Hint

Think about what the server must do to support conditional GET requests.

🚀 Application
expert
3:00remaining
Implementing conditional GET with Last-Modified in pseudocode

Given a REST API endpoint, which pseudocode snippet correctly implements conditional GET using Last-Modified and If-Modified-Since headers?

A
if request.headers['If-Modified-Since'] &gt;= resource.last_modified:
    return 304 Not Modified
else:
    return 200 OK with resource and Last-Modified header
B
if request.headers['If-Modified-Since'] &lt; resource.last_modified:
    return 304 Not Modified
else:
    return 200 OK with resource and Last-Modified header
Creturn 200 OK with resource and ignore If-Modified-Since header
D
if resource.last_modified == None:
    return 304 Not Modified
else:
    return 200 OK with resource
Attempts:
2 left
💡 Hint

Remember the server returns 304 only if the resource was not modified after the date the client provides.