HTTP status code 428 indicates that the server requires the client to meet certain preconditions in order to process the request. This status code was introduced in 2012 through RFC 6585.
Key Features and Use Cases
- Purpose: Prevent clients from sending requests when specific conditions are not met.
- Common Uses:
- Preventing conflicts when multiple clients attempt to update the same resource simultaneously.
- Avoiding “lost update” issues.
Implementation
The server typically requires the client to use conditional request headers, such as If-Match
or If-Unmodified-Since
.
Client Response
Upon receiving this status code, the client must include the necessary preconditions in the request and resend it.
Security
Using this status code can prevent accidental updates or overwrites, helping to maintain data consistency.
Specific Situations
This status code is particularly useful in:
- Multi-user collaborative environments.
- Applications where data consistency is critical.
- Scenarios requiring optimistic locking.
Example Scenario
Consider an online document editing system where multiple users might edit the same document simultaneously. To prevent users from unintentionally overwriting each other’s changes, the server may require that each update request includes the current version number of the document. If the version number does not match, the server will return a 428 status code, prompting the client to first retrieve the latest version before making edits.
PUT /document/123 HTTP/1.1
Host: example.com
If-Match: "e0023aa4e"
{"title": "Updated Title", "content": "New content..."}
If the client does not include the If-Match
header, or if the provided ETag does not match, the server may respond:
HTTP/1.1 428 Precondition Required
Content-Type: application/json
{
"error": "Precondition Required",
"message": "Please provide a valid If-Match header with the current ETag of the resource."
}
This mechanism helps to ensure the consistency and integrity of data, especially in distributed systems or high concurrency environments.
Related 4xx error code
- 400 Bad Request
- 401 Unauthorized
- 402 Payment Required
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 406 Not Acceptable
- 407 Proxy Authentication Required
- 408 Request Timeout
- 409 Conflict
- 410 Gone
- 411 Length Required
- 412 Precondition Failed
- 413 Payload Too Large
- 414 URI Too Long
- 415 Unsupported Media Type
- 416 Range Not Satisfiable
- 417 Expectation Failed
- 418 I’m a teapot
- 421 Misdirected Request
- 422 Unprocessable Entity (WebDAV)
- 423 Locked (WebDAV)
- 424 Failed Dependency (WebDAV)
- 425 Too Early
- 426 Upgrade Required
- 429 Too Many Requests
- 431 Request Header Fields Too Large
- 451 Unavailable For Legal Reasons
- 499 Client Closed Request