These help your app avoid downloading data again if it hasn't changed. This saves time and internet data.
If-None-Match and 304 responses in Rest API
Client sends header: If-None-Match: "etag_value" Server responds: - 304 Not Modified if data is unchanged - 200 OK with data if changed
The If-None-Match header contains an ETag value, which is like a fingerprint of the data.
A 304 Not Modified response means the client can use its cached copy.
image.png but only if it has changed since the version with ETag abc123etag.GET /image.png HTTP/1.1 Host: example.com If-None-Match: "abc123etag"
HTTP/1.1 304 Not Modified
HTTP/1.1 200 OK ETag: "def456etag" Content-Type: image/png (binary image data)
This program makes two requests to a server that supports ETag. The second request uses If-None-Match with the ETag from the first response. If the resource is unchanged, the server replies with 304, telling the client to use cached data.
import requests url = 'https://httpbin.org/etag/abc123etag' # First request to get the resource and ETag response1 = requests.get(url) print('First request status:', response1.status_code) print('ETag received:', response1.headers.get('ETag')) # Second request with If-None-Match header etag = response1.headers.get('ETag') headers = {'If-None-Match': etag} response2 = requests.get(url, headers=headers) print('Second request status:', response2.status_code) if response2.status_code == 304: print('Resource not modified, use cached data') else: print('Resource changed, new data received')
ETags are usually strings that uniquely identify a version of a resource.
Not all servers support ETags or 304 responses, so check your API documentation.
Using If-None-Match helps reduce data usage and speeds up your app.
If-None-Match lets clients ask if data changed by sending an ETag.
304 Not Modified means the data is the same and the client can use its cached copy.
This mechanism saves time, data, and server work by avoiding unnecessary downloads.
Practice
If-None-Match header do in a REST API request?Solution
Step 1: Understand the purpose of
This header sends the ETag value from the client to check if the resource has changed.If-None-MatchStep 2: Determine the server's response based on the ETag
If the ETag matches, the server returns304 Not Modified, meaning no new data is sent.Final Answer:
It asks the server to send data only if it has changed since the last request. -> Option AQuick Check:
If-None-Match checks changes [OK]
- Thinking it forces fresh data every time
- Confusing it with cache deletion
- Assuming it always sends full data
If-None-Match header with ETag value "abc123" in an HTTP request?Solution
Step 1: Recall HTTP header syntax
Headers use colon:to separate name and value, and ETag values are quoted strings.Step 2: Identify correct quoting and separator
If-None-Match: "abc123" uses colon and quotes correctly:If-None-Match: "abc123".Final Answer:
If-None-Match: "abc123" -> Option CQuick Check:
Headers use colon + quoted ETag [OK]
- Omitting quotes around ETag
- Using equals or arrow instead of colon
- Not quoting the ETag string
if request.headers.get('If-None-Match') == current_etag:
return 304
else:
return 200 with data and ETagWhat will the server respond if the client sends
If-None-Match: "xyz789" but current_etag is "abc123"?Solution
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.Step 2: Determine server response based on mismatch
Since ETags differ, the server sends full data with status 200 and the current ETag.Final Answer:
200 OK with data and ETag -> Option AQuick Check:
ETags differ -> send data [OK]
- Assuming 304 when ETags differ
- Confusing 400 or 500 errors with ETag mismatch
- Ignoring quotes in ETag comparison
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 304What is the main error?
Solution
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.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.Final Answer:
The 200 and 304 status codes are swapped in the if-else blocks. -> Option DQuick Check:
ETag match -> 304, not 200 [OK]
- Returning 200 on ETag match
- Confusing header access methods
- Ignoring quotes in ETag comparison
If-None-Match. Which of the following sequences correctly describes the client-server interaction to achieve this?Solution
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.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.Final Answer:
Client sends request without ETag -> Server sends data with ETag -> Client caches data and ETag -> Client sends request withIf-None-Matchheader -> Server returns 304 if data unchanged -> Option BQuick Check:
Proper If-None-Match flow [OK]
- Sending ETag in request body instead of header
- Expecting 304 without sending If-None-Match
- Ignoring caching after 304 response
Ready to prove it?
Pass the test and this pattern moves unknown
