Predict the output
The client sends If-Match with an old ETag. What does the handler return?
def update(current_version: int, if_match: str) -> int:
if if_match != f'"v{current_version}"':
return 412
return 200
print(update(current_version=8, if_match='"v7"'))Hint 1 of 1
Which version did the client read?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 412
Correct. The resource moved to v8 since the client read v7, so the update is refused.
If-Match turns the update into compare-and-set: it only applies while the resource is still the version the client read. A mismatch means someone else wrote first, and 412 Precondition Failed says so.
Why not the others?
- B
200. That would overwrite someone else's change. - C
304. 304 answers conditional GETs, not updates. - D
409. Possible in some APIs, but If-Match failures are defined as 412.