428 Precondition Required means the server requires this request to be conditional. If a client sends a write without a precondition header like If-Match, the server refuses it and demands one.
The goal is to prevent the lost-update problem: if two clients each fetch the same resource and then save in turn, the later save silently overwrites the earlier one. By requiring If-Match, the server blocks such unconditional overwrites.
PUT /docs/42 HTTP/1.1
Host: api.example.com
Content-Type: application/json
{"title":"New"}HTTP/1.1 428 Precondition Required
Content-Type: application/json
{"error":"precondition_required","hint":"Include an If-Match header with the current ETag."}app.put('/docs/:id', (req, res) => {
if (!req.headers['if-match']) {
return res.status(428).json({
error: 'precondition_required',
hint: 'Include an If-Match header with the current ETag.'
});
}
// proceed with the conditional update...
});