HTTP 412 Precondition Failed
4xx Client Error
- Home
- HTTP Status Codes
- 4xx
- 412
What is HTTP 412 Precondition Failed?
The 412 (Precondition Failed) status code indicates that one or more conditions given in the request header fields evaluated to false when tested on the server. This is used with conditional requests using headers like If-Match, If-None-Match, If-Modified-Since, or If-Unmodified-Since.
Common Use Cases
- Optimistic concurrency control with If-Match
- Conditional updates to prevent overwriting changes
- Cache validation with ETag conditions
Usage Example
When updating a resource, include the ETag of the current version in the If-Match header. If another client has modified the resource since the ETag was generated, the server returns 412 Precondition Failed, preventing the overwrite. The client must fetch the latest version and retry.
// Laravel - conditional update with If-Match
$post = Post::findOrFail($id);
$etag = md5($post->updated_at->timestamp);
if ($request->header('If-Match') !== '"' . $etag . '"') {
return response()->json([
'error' => 'Precondition Failed',
'current_etag' => $etag,
], 412);
}
Related Status Codes
Common Mistakes
Mistake: Confusing 412 (precondition failed) with 409 (conflict)
Fix: Use 412 when a conditional header (If-Match, If-Unmodified-Since) explicitly fails. Use 409 when the request conflicts with the resource state but no conditional header was involved.
Mistake: Not providing the current ETag in the 412 response
Fix: Include the current ETag or timestamp in the 412 response so the client can update its local state and retry the request with the correct precondition header.