HTTP 412 Precondition Failed indicates that one or more conditions in the request headers (If-Match, If-Unmodified-Since, If-None-Match) evaluated to false on the server. This is the backbone of optimistic concurrency control in HTTP — the client says 'only update this if it hasn't changed since I last saw it,' and the server returns 412 when it has changed, preventing lost updates.
Response includes the status code, standard headers (including Content-Type), and a small diagnostic JSON body describing the request and returned status.
Simulator URL (copy in the app after load — not a normal link):
https://httpstatus.com/api/status/412
Example request:
curl -i "https://httpstatus.com/api/status/412"The server does not meet one of the preconditions that the requester put on the request.
On this code, Inspector focuses on semantics, headers, and correctness warnings that commonly affect clients and caches.
HTTP 412 Precondition Failed has specific technical implications for API design, caching, and client behavior. Understanding the precise semantics helps distinguish it from similar status codes and implement correct error handling. The response should include a descriptive body following a consistent error schema (like RFC 7807 Problem Details) so clients can programmatically handle the error.
// Handle 412 Precondition Failed in Express
app.use((err, req, res, next) => {
if (err.status === 412) {
return res.status(412).json({
type: 'https://api.example.com/errors/precondition-failed',
title: 'Precondition Failed',
status: 412,
detail: err.message
});
}
next(err);
});from fastapi import HTTPException
# Raise 412 Precondition Failed
raise HTTPException(
status_code=412,
detail={
'type': 'precondition_failed',
'message': 'Descriptive error for 412 Precondition Failed'
}
)// Spring Boot 412 Precondition Failed handling
@ExceptionHandler(CustomPreconditionFailedException.class)
public ResponseEntity<ErrorResponse> handlePreconditionFailed(
CustomPreconditionFailedException ex) {
return ResponseEntity.status(412)
.body(new ErrorResponse("Precondition Failed", ex.getMessage()));
}// Return 412 Precondition Failed
func errorHandler(w http.ResponseWriter, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(412)
json.NewEncoder(w).Encode(map[string]any{
"status": 412,
"error": "Precondition Failed",
"message": message,
})
}