0
0
Rest APIprogramming~20 mins

ETag for conditional requests in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ETag Mastery
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 when the ETag matches?

Consider a REST API endpoint that returns a resource with an ETag header. A client sends a GET request with the header If-None-Match set to the current ETag value of the resource.

What HTTP status code will the server return if the resource has not changed?

Rest API
GET /resource HTTP/1.1
Host: example.com
If-None-Match: "abc123etag"
A404 Not Found
B200 OK with the full resource body
C304 Not Modified with no body
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Think about what the server does when the resource has not changed since the client last fetched it.

🧠 Conceptual
intermediate
1:30remaining
What is the purpose of the ETag header in HTTP?

Choose the best description of what the ETag header does in HTTP responses.

AIt provides a unique identifier for a specific version of a resource.
BIt specifies the content type of the response body.
CIt indicates the server's IP address.
DIt sets the cache expiration time.
Attempts:
2 left
💡 Hint

ETag helps clients know if the resource has changed or not.

🔧 Debug
advanced
2:30remaining
Why does this conditional GET request always return 200 OK instead of 304 Not Modified?

Given the following server code snippet handling ETag conditional requests, identify the bug causing the server to always return 200 OK even when the resource is unchanged.

Rest API
etag = get_current_etag(resource)
if request.headers.get('If-None-Match') == etag:
    return resource, 304, {'ETag': etag}
else:
    return resource, 200, {'ETag': etag}
AThe server should return 304 status code when ETags match, not 200.
BThe server is missing the ETag header in the response.
CThe server should compare If-Match header instead of If-None-Match.
DThe server should always return 404 if ETags don't match.
Attempts:
2 left
💡 Hint

Check the status code returned when the ETags match.

📝 Syntax
advanced
1:30remaining
Which HTTP header syntax is correct for sending an ETag in a response?

Choose the correct syntax for the ETag header in an HTTP response.

AETag: abc123etag
BETag: 'abc123etag'
CETag = "abc123etag"
DETag: "abc123etag"
Attempts:
2 left
💡 Hint

ETag values are usually quoted strings.

🚀 Application
expert
2:00remaining
How many items are in the cache after these conditional requests with ETags?

A client makes three GET requests to a REST API for the same resource with ETag "v1". The server responds:

  • First request: 200 OK with ETag "v1"
  • Second request: If-None-Match: "v1" → 304 Not Modified
  • Third request: If-None-Match: "v1" → 304 Not Modified

How many copies of the resource are stored in the client's cache after these requests?

A2 copies
B1 copy
C0 copies
D3 copies
Attempts:
2 left
💡 Hint

Think about how caching works with 304 responses.