Bird
Raised Fist0
Rest APIprogramming~10 mins

If-None-Match and 304 responses 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 check if the client's ETag matches the server's ETag.

Rest API
if request.headers.get('If-None-Match') == [1]:
    return '', 304
Drag options to blanks, or click blank then click option'
Aserver_etag
Bclient_etag
Cetag_value
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using the client's ETag variable instead of the server's ETag for comparison.
Comparing to None instead of the server's ETag.
2fill in blank
medium

Complete the code to set the ETag header in the server's response.

Rest API
response.headers['ETag'] = [1]
Drag options to blanks, or click blank then click option'
Aserver_etag
BNone
Cetag_value
Dclient_etag
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the ETag header to the client's ETag value.
Leaving the ETag header unset.
3fill in blank
hard

Fix the error in the code that returns a 304 response when ETags match.

Rest API
if request.headers.get('If-None-Match') == [1]:
    return '', 304
Drag options to blanks, or click blank then click option'
Aetag_value
Bclient_etag
Cserver_etag
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 200 instead of 304 when ETags match.
Comparing to the wrong ETag variable.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps resource IDs to their ETags only if the ETag length is greater than 10.

Rest API
etag_map = {resource_id: [1] for resource_id, [2] in resources.items() if len(etag) > 10}
Drag options to blanks, or click blank then click option'
Aetag
Betag_value
Dresource_etag
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names for ETag in the comprehension.
Using the wrong variable in the if condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase resource IDs to their ETags only if the ETag starts with 'W/'.

Rest API
etag_map = { [1]: [2] for resource_id, etag in resources.items() if etag.[3]('W/') }
Drag options to blanks, or click blank then click option'
Aresource_id.upper()
Betag
Cstartswith
Dendswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using endswith instead of startswith.
Not converting resource IDs to uppercase.

Practice

(1/5)
1. What does the If-None-Match header do in a REST API request?
easy
A. It asks the server to send data only if it has changed since the last request.
B. It forces the server to ignore caching and send fresh data every time.
C. It tells the server to delete the cached data on the client side.
D. It requests the server to send the full data regardless of changes.

Solution

  1. Step 1: Understand the purpose of If-None-Match

    This header sends the ETag value from the client to check if the resource has changed.
  2. Step 2: Determine the server's response based on the ETag

    If the ETag matches, the server returns 304 Not Modified, meaning no new data is sent.
  3. Final Answer:

    It asks the server to send data only if it has changed since the last request. -> Option A
  4. Quick Check:

    If-None-Match checks changes [OK]
Hint: If-None-Match checks if data changed before sending [OK]
Common Mistakes:
  • Thinking it forces fresh data every time
  • Confusing it with cache deletion
  • Assuming it always sends full data
2. Which of the following is the correct syntax for sending an If-None-Match header with ETag value "abc123" in an HTTP request?
easy
A. If-None-Match: abc123
B. If-None-Match = "abc123"
C. If-None-Match: "abc123"
D. If-None-Match => abc123

Solution

  1. Step 1: Recall HTTP header syntax

    Headers use colon : to separate name and value, and ETag values are quoted strings.
  2. Step 2: Identify correct quoting and separator

    If-None-Match: "abc123" uses colon and quotes correctly: If-None-Match: "abc123".
  3. Final Answer:

    If-None-Match: "abc123" -> Option C
  4. Quick Check:

    Headers use colon + quoted ETag [OK]
Hint: Headers use colon and quotes for ETag values [OK]
Common Mistakes:
  • Omitting quotes around ETag
  • Using equals or arrow instead of colon
  • Not quoting the ETag string
3. Given this server logic:
if request.headers.get('If-None-Match') == current_etag:
    return 304
else:
    return 200 with data and ETag

What will the server respond if the client sends If-None-Match: "xyz789" but current_etag is "abc123"?
medium
A. 200 OK with data and ETag
B. 304 Not Modified
C. 400 Bad Request
D. 500 Internal Server Error

Solution

  1. Step 1: Compare client's ETag with server's current ETag

    The client sends "xyz789" but the server's current ETag is "abc123", so they do not match.
  2. Step 2: Determine server response based on mismatch

    Since ETags differ, the server sends full data with status 200 and the current ETag.
  3. Final Answer:

    200 OK with data and ETag -> Option A
  4. Quick Check:

    ETags differ -> send data [OK]
Hint: If ETags differ, server sends full data (200) [OK]
Common Mistakes:
  • Assuming 304 when ETags differ
  • Confusing 400 or 500 errors with ETag mismatch
  • Ignoring quotes in ETag comparison
4. A developer wrote this code snippet to handle If-None-Match but it always returns 200 OK even when ETags match:
if request.headers['If-None-Match'] == current_etag:
    return 200, data
else:
    return 304

What is the main error?
medium
A. The server must always return 200 regardless of ETag.
B. The code should compare request.headers.get() instead of direct indexing.
C. The ETag values should be stripped of quotes before comparison.
D. The 200 and 304 status codes are swapped in the if-else blocks.

Solution

  1. Step 1: Analyze the if condition and returned status codes

    The code returns 200 OK when ETags match, but it should return 304 Not Modified instead.
  2. Step 2: Identify correct status code usage for ETag match

    When ETags match, server returns 304 to avoid sending data; else returns 200 with data.
  3. Final Answer:

    The 200 and 304 status codes are swapped in the if-else blocks. -> Option D
  4. Quick Check:

    ETag match -> 304, not 200 [OK]
Hint: 304 means no change; 200 means send data [OK]
Common Mistakes:
  • Returning 200 on ETag match
  • Confusing header access methods
  • Ignoring quotes in ETag comparison
5. You want to optimize your REST API to reduce bandwidth by using If-None-Match. Which of the following sequences correctly describes the client-server interaction to achieve this?
hard
A. Client sends request with If-None-Match -> Server always returns 200 with data and ETag -> Client caches data
B. Client sends request without ETag -> Server sends data with ETag -> Client caches data and ETag -> Client sends request with If-None-Match header -> Server returns 304 if data unchanged
C. Client sends request with ETag in body -> Server returns 304 if data unchanged -> Client discards cached data
D. Client sends request without headers -> Server returns 304 if data unchanged -> Client updates cache

Solution

  1. Step 1: Understand the correct use of If-None-Match and 304

    The client first gets data and ETag, caches them, then sends If-None-Match with ETag to check for changes.
  2. Step 2: Identify the correct sequence

    The sequence that starts with an initial request without the ETag header, receives data and ETag from the server, caches both, and then uses If-None-Match on future requests, receiving 304 if unchanged, is correct.
  3. Final Answer:

    Client sends request without ETag -> Server sends data with ETag -> Client caches data and ETag -> Client sends request with If-None-Match header -> Server returns 304 if data unchanged -> Option B
  4. Quick Check:

    Proper If-None-Match flow [OK]
Hint: Initial fetch no ETag, then use If-None-Match with ETag [OK]
Common Mistakes:
  • Sending ETag in request body instead of header
  • Expecting 304 without sending If-None-Match
  • Ignoring caching after 304 response
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