Bird
Raised Fist0
Rest APIprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of an ETag in REST APIs?

easy
A. To help clients check if the resource has changed since last fetch
B. To encrypt the data sent from server to client
C. To authenticate the client making the request
D. To specify the content type of the response

Solution

  1. Step 1: Understand ETag role in REST

    ETags are unique identifiers for resource versions, used to detect changes.
  2. Step 2: Match purpose with options

    Only To help clients check if the resource has changed since last fetch describes checking if resource changed, which is the ETag's purpose.
  3. Final Answer:

    To help clients check if the resource has changed since last fetch -> Option A
  4. Quick Check:

    ETag purpose = freshness check [OK]
Hint: ETags track changes, not encrypt or authenticate [OK]
Common Mistakes:
  • Confusing ETag with authentication tokens
  • Thinking ETag encrypts data
  • Assuming ETag sets content type
2.

Which HTTP header should a client send to make a conditional GET request using an ETag?

easy
A. If-Modified-Since
B. If-None-Match
C. ETag
D. Cache-Control

Solution

  1. Step 1: Identify client header for ETag conditional requests

    The client sends If-None-Match header with the ETag value to check freshness.
  2. Step 2: Compare options

    If-Modified-Since uses dates, ETag is a response header, Cache-Control controls caching rules. Only If-None-Match fits.
  3. Final Answer:

    If-None-Match -> Option B
  4. Quick Check:

    Client uses If-None-Match for ETag check [OK]
Hint: Client sends If-None-Match with ETag to check freshness [OK]
Common Mistakes:
  • Using ETag header in request instead of response
  • Confusing If-Modified-Since with ETag usage
  • Sending Cache-Control instead of conditional header
3.

Given this HTTP interaction:
GET /resource HTTP/1.1
Host: example.com
If-None-Match: "abc123"

Server responds:
HTTP/1.1 304 Not Modified
What does this response mean?

medium
A. The resource has changed and new data is sent
B. The server encountered an error
C. The resource has not changed; client should use cached data
D. The request is unauthorized

Solution

  1. Step 1: Understand 304 Not Modified status

    304 means resource is unchanged since client's ETag, so no new data sent.
  2. Step 2: Match meaning with options

    The resource has not changed; client should use cached data correctly states client should use cached data because resource is unchanged.
  3. Final Answer:

    The resource has not changed; client should use cached data -> Option C
  4. Quick Check:

    304 = Not Modified, use cache [OK]
Hint: 304 means no change; use cached copy [OK]
Common Mistakes:
  • Thinking 304 means new data sent
  • Confusing 304 with error or unauthorized status
  • Ignoring ETag and caching role
4.

Consider this server code snippet handling ETags:

etag = generate_etag(resource)
if request.headers.get('If-None-Match') == etag:
return Response(status=304)
else:
return Response(data=resource, headers={'ETag': etag})

What is a potential bug in this code?

medium
A. It compares ETag strings without quotes, causing mismatch
B. It returns 304 even if ETag does not match
C. It does not handle If-Modified-Since header
D. It always returns data ignoring conditional requests

Solution

  1. Step 1: Analyze ETag comparison

    ETags in headers often include quotes, so direct string equality may fail if quotes differ.
  2. Step 2: Identify mismatch cause

    If client sends "abc123" but server etag is abc123 without quotes, comparison fails incorrectly.
  3. Final Answer:

    It compares ETag strings without quotes, causing mismatch -> Option A
  4. Quick Check:

    ETag quotes matter in comparison [OK]
Hint: ETag strings must match exactly including quotes [OK]
Common Mistakes:
  • Ignoring quotes around ETag values
  • Assuming If-Modified-Since is required for ETag logic
  • Returning 304 without checking ETag
5.

You want to optimize your REST API to reduce bandwidth by using ETags. Which approach correctly implements conditional GET requests with ETags?

1. Server generates ETag for resource.
2. Server sends ETag in response header.
3. Client stores ETag.
4. Client sends GET with If-None-Match header.
5. Server compares ETag and returns 304 if unchanged.
6. Otherwise, server sends new data with new ETag.

Which step is missing or incorrect?

hard
A. Step 6: Server should always send 200 status even if unchanged
B. Step 4: Client should send If-Modified-Since instead of If-None-Match
C. Step 3: Client should not store ETag
D. Step 5: Server must compare ETag including quotes exactly

Solution

  1. Step 1: Review each step for correctness

    Steps 1-4 are correct standard flow for ETag conditional requests.
  2. Step 2: Identify subtle issue in step 5

    Server must compare ETag values exactly, including quotes, or else conditional check fails.
  3. Step 3: Confirm other options are incorrect

    Client should store ETag (step 3), If-None-Match is correct header (step 4), and 304 status is correct for unchanged (step 6).
  4. Final Answer:

    Step 5: Server must compare ETag including quotes exactly -> Option D
  5. Quick Check:

    Exact ETag match including quotes is required [OK]
Hint: ETag comparison must be exact including quotes [OK]
Common Mistakes:
  • Ignoring quotes in ETag comparison
  • Using wrong conditional header
  • Sending 200 instead of 304 for unchanged resource
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