0
0
Rest APIprogramming~10 mins

ETag for conditional requests in Rest API - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ETag for conditional requests
Client sends GET request
Server checks resource state
Server generates ETag for resource
Server sends response with ETag header
Client stores ETag
Client sends conditional GET with If-None-Match: ETag
Server compares ETag with current resource
ETag matches
Send 304 Not Modified
The client requests a resource, server sends it with an ETag. Later, client asks conditionally using that ETag. Server replies 304 if unchanged or 200 with new data if changed.
Execution Sample
Rest API
GET /resource HTTP/1.1
Host: example.com

--- Server Response ---
HTTP/1.1 200 OK
ETag: "abc123"
Content: {"data": "value"}

--- Conditional Request ---
GET /resource HTTP/1.1
If-None-Match: "abc123"
Host: example.com
Client first gets resource with ETag, then sends conditional request with If-None-Match header.
Execution Table
StepRequest TypeETag Sent by ClientServer ETagETag Match?Server Response
1Initial GETNone"abc123"N/A200 OK with resource and ETag "abc123"
2Conditional GET"abc123""abc123"Yes304 Not Modified, no body
3Conditional GET"abc123""def456"No200 OK with new resource and ETag "def456"
💡 Execution stops after server responds based on ETag match.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
Client ETagNone"abc123""abc123""abc123" or updated to "def456" if resource changed
Server Resource State"value""value""value""new value" if changed
Server ETagN/A"abc123""abc123""def456"
Key Moments - 3 Insights
Why does the server send 304 Not Modified instead of 200 OK?
Because the client's If-None-Match ETag matches the server's current ETag (see execution_table row 2), meaning the resource has not changed.
What happens if the resource changes on the server?
The server generates a new ETag (see execution_table row 3), so the ETag does not match the client's. The server sends 200 OK with the updated resource and new ETag.
Why does the client send the ETag in the If-None-Match header?
To ask the server if the resource has changed since last fetch, enabling efficient caching and saving bandwidth if unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response does the server send at step 2?
A200 OK with new resource
B304 Not Modified
C404 Not Found
D500 Internal Server Error
💡 Hint
Check the 'Server Response' column in execution_table row 2.
At which step does the server detect that the resource has changed?
AStep 1
BStep 2
CStep 3
DNone of the above
💡 Hint
Look at the 'ETag Match?' column and 'Server Response' in execution_table.
If the client sends a different ETag in If-None-Match at step 2, what changes in the execution_table?
AServer responds 304 Not Modified
BServer responds 200 OK with new resource
CClient ETag becomes None
DServer ETag becomes None
💡 Hint
Refer to execution_table row 3 where ETag does not match.
Concept Snapshot
ETag is a unique identifier for a resource version.
Client stores ETag from server response.
Client sends If-None-Match with ETag to check freshness.
Server compares ETag; if match, sends 304 Not Modified.
If no match, sends 200 OK with new resource and ETag.
This saves bandwidth by avoiding sending unchanged data.
Full Transcript
This visual execution shows how ETag works for conditional requests in REST APIs. First, the client requests a resource. The server responds with the resource and an ETag header, a unique tag representing the resource version. The client saves this ETag. Later, the client sends a conditional GET request with the If-None-Match header containing the saved ETag. The server compares this ETag with the current resource's ETag. If they match, the server replies with 304 Not Modified, meaning the resource has not changed, so the client can use its cached copy. If they differ, the server sends 200 OK with the updated resource and a new ETag. This process helps reduce data transfer and improves efficiency by avoiding sending unchanged data.