Bird
Raised Fist0
Rest APIprogramming~5 mins

If-None-Match and 304 responses in Rest API

Choose your learning style10 modes available
Introduction

These help your app avoid downloading data again if it hasn't changed. This saves time and internet data.

When your app asks a server for a file but wants to check if it changed first.
When you want to make your website faster by not reloading images or data if they are the same.
When you build a mobile app that should save battery and data by not downloading repeated info.
When you want to reduce server load by letting it skip sending unchanged data.
When you want to keep your app data fresh but avoid unnecessary downloads.
Syntax
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.

Examples
The client asks for image.png but only if it has changed since the version with ETag abc123etag.
Rest API
GET /image.png HTTP/1.1
Host: example.com
If-None-Match: "abc123etag"
The server says the image is the same, so the client should use its cached copy.
Rest API
HTTP/1.1 304 Not Modified
The server sends the new image with a new ETag because it has changed.
Rest API
HTTP/1.1 200 OK
ETag: "def456etag"
Content-Type: image/png

(binary image data)
Sample Program

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.

Rest API
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')
OutputSuccess
Important Notes

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.

Summary

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

(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