The If-Unmodified-Since
header is an HTTP request header that allows the client to specify a condition for the requested resource. This header field is used to make the request conditional, meaning that the server will only process the request if the resource has not been modified since the specified date and time.
Purpose
The primary purpose of the If-Unmodified-Since
header is to ensure that the requested resource has not changed since a specific date and time. This is particularly useful when clients want to perform actions such as updating or deleting a resource but only if it has not been modified by another client or process.
By using the If-Unmodified-Since
header, clients can avoid potential conflicts and data loss that could occur when multiple clients attempt to modify the same resource simultaneously.
Syntax
The If-Unmodified-Since
header field uses the following syntax:
If-Unmodified-Since: <http-date>
The <http-date>
is an HTTP-date timestamp, as defined in RFC 7231, section 7.1.1.1. It represents the date and time since which the resource should not have been modified.
Example Usage
Request Example
In this example, the client wants to update a resource located at /example-resource
but only if it has not been modified since the specified date and time.
PUT /example-resource HTTP/1.1
Host: example.com
Content-Type: application/json
If-Unmodified-Since: Tue, 22 Jun 2022 14:28:00 GMT
{
"key": "new-value"
}
Response Examples
If the resource has not been modified since the specified date and time, the server will process the request and return a 204 No Content
status code, indicating that the update was successful.
HTTP/1.1 204 No Content
Date: Wed, 23 Jun 2022 10:00:00 GMT
However, if the resource has been modified since the specified date and time, the server will return a 412 Precondition Failed
status code, indicating that the precondition set by the If-Unmodified-Since
header has not been met.
HTTP/1.1 412 Precondition Failed
Date: Wed, 23 Jun 2022 10:00:00 GMT
Summary
The If-Unmodified-Since
header is an important tool for ensuring the consistency and integrity of resources in a concurrent environment. By making requests conditional based on the modification date of a resource, clients can prevent conflicts and data loss that may occur when multiple clients attempt to modify the same resource simultaneously.
Remember to always consider the use of the If-Unmodified-Since
header when implementing HTTP clients that interact with resources that could be subject to concurrent modifications.