CodePark/ questions / Status codes
START PRACTICINGPRACTICE
Backend question · HTTP & API design › Status codes

A client sends this request, but the document's current ETag is "v4". What should the server respond?

PUT /docs/12 HTTP/1.1
Host: api.example.com
If-Match: "v3"
Content-Type: application/json

{"title":"New title"}
412 Precondition Failed

The server should answer 412 Precondition Failed. The If-Match condition is false, because the client's ETag does not match the current "v4". This is exactly what 412 reports, and the server does not apply the update.

"428 Precondition Required" is wrong. The server sends 428 when it requires a conditional request and the client sent no condition at all. "409 Conflict" is wrong too. The server sends 409 for conflicts it finds in the resource state, without a precondition header.