Bird
Raised Fist0
Rest APIprogramming~10 mins

If-None-Match and 304 responses in Rest API - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - If-None-Match and 304 responses
Client sends GET request with If-None-Match header
↓
Server checks resource ETag
↓
ETag matches
↓
Send 304 Not Modified
↓
Client uses cached
↓
END
The client sends a request with an ETag in If-None-Match. The server compares it to the current resource ETag. If they match, server replies 304 (no new data). Otherwise, server sends the full resource with 200.
Execution Sample
Rest API
GET /resource HTTP/1.1
If-None-Match: "abc123"

Server checks ETag: "abc123"
Response: 304 Not Modified
Client requests a resource with an ETag. Server sees ETag matches current version and replies 304 to save bandwidth.
Execution Table
StepClient RequestServer ETagETag Match?Server ResponseClient Action
1GET /resource with If-None-Match: "abc123""abc123"Yes304 Not ModifiedUse cached resource
2GET /resource with If-None-Match: "xyz789""abc123"No200 OK with resource bodyUpdate cache with new resource
3GET /resource without If-None-Match"abc123"N/A200 OK with resource bodyCache resource
💡 Requests end after server responds with either 304 or 200 depending on ETag match
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
Client If-None-MatchNone"abc123""xyz789"None
Server ETag"abc123""abc123""abc123""abc123"
Server ResponseNone304 Not Modified200 OK with resource body200 OK with resource body
Client CacheEmptyCached resource with ETag "abc123"Updated resource with ETag "abc123"Cached resource with ETag "abc123"
Key Moments - 3 Insights
Why does the server send a 304 response instead of the full resource?
Because the client's If-None-Match header matches the server's current ETag (see execution_table step 1), meaning the client already has the latest version.
What happens if the client sends no If-None-Match header?
The server treats it as a new request and sends the full resource with 200 OK (see execution_table step 3).
Why does the client update its cache after a 200 OK response?
Because the server sent a new resource version (ETag did not match), so the client replaces its cached copy with the fresh data (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the server response when the client's If-None-Match matches the server's ETag?
A304 Not Modified
B200 OK with resource body
C404 Not Found
D500 Internal Server Error
💡 Hint
Check execution_table row 1 under 'Server Response'
At which step does the client update its cache with a new resource?
AStep 1
BStep 2
CStep 3
DNone of the steps
💡 Hint
Look at execution_table 'Client Action' column for cache updates
If the client sends no If-None-Match header, what response does the server send?
A400 Bad Request
B304 Not Modified
C200 OK with resource body
D401 Unauthorized
💡 Hint
See execution_table step 3 for requests without If-None-Match
Concept Snapshot
If-None-Match header lets client send ETag to server.
Server compares ETag with current resource version.
If match, server replies 304 Not Modified (no body).
If no match or missing header, server sends 200 OK with resource.
This saves bandwidth by avoiding sending unchanged data.
Full Transcript
This visual trace shows how the If-None-Match header works with 304 responses in REST APIs. The client sends a GET request with an If-None-Match header containing an ETag value. The server compares this ETag with the current resource's ETag. If they match, the server responds with 304 Not Modified, telling the client to use its cached copy. If they don't match or the header is missing, the server sends the full resource with a 200 OK response. The execution table walks through three steps: a matching ETag request, a non-matching ETag request, and a request without the header. The variable tracker shows how the client's If-None-Match, server's ETag, server response, and client cache change over these steps. Key moments clarify why 304 is sent, what happens without the header, and when the client updates its cache. The quiz tests understanding of these steps and responses. This helps beginners see how conditional requests save bandwidth and improve efficiency in web APIs.

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