PATCH Method
- Category :
- Concept
- Tags :
- general_computing_gleanings
- http
- http_method
- p
- Created:
About1
The PATCH HTTP method (HTTP verb) applies partial modifications to a resource.
PATCH is similar to the ‘update’ concept found in CRUD (in general, HTTP is different than CRUD, and the two should not be confused).
Also known as a PATCH request.
Comparison of PUT and PATCH
- A
PATCHserves as a set of instructions for modifying a resource, whereas PUT method represents a complete replacement of the resource. - A
PUTrequest is always idempotent (repeating the same request multiple times results in the resource remaining in the same state), whereas aPATCHrequest may not always be idempotent. For instance, if a resource includes an auto-incrementing counter, aPUTrequest will overwrite the counter (since it replaces the entire resource), but aPATCHrequest may not.
Comparison of PATCH and other methods
Like a POST method, a PATCH can potentially have side effects on other resources.
A server can advertise support for PATCH by adding it to the list in the Allow or
Access-Control-Allow-Methods (for Cross-Origin Resource Sharing (CORS) response headers.
Another implicit indication that PATCH is supported is the Accept-Patch header (usually after
an OPTIONS request on a resource), which lists the media-types the server is able to understand
in a PATCH request for a resource.
Characteristics
| Request has body | Successful response has body | Safe | Idempotent | Cacheable | Allowed in HTML forms |
|---|---|---|---|---|---|
| Yes | May | No | No | Only if freshness information is included | No |
Syntax
PATCH <request-target>["?"<query>] HTTP/1.1
<request-target>: identifies the target resource of the request when combined with the information provided in the Host header. This is an absolute path (e.g./path/to/file.html) in requests to an origin server, and an absolute URL in requests to proxies (e.g.http://www.example.com/path/to/file.html)<query>(Optional) - an optional query component preceded by a question-mark?. Often used to carry identifying information in the form ofkey=valuepairs.
Examples
Successfully modifying a resource
Assume there is a resource on the server representing a user with a numeric ID of 123 in the following format:
{
"firstName": "Example",
"LastName": "User",
"userId": 123,
"signupDate": "2024-09-09T21:48:58Z",
"status": "active",
"registeredDevice": {
"id": 1,
"name": "personal",
"manufacturer": {
"name": "Hardware corp"
}
}
}
Instead of sending a JSON object to fully overwrite a resource, a PATCH modifies only specific parts of the resource. This
request updates the status filed:
PATCH /users/123 HTTP/1.1
Host: example.com
Content-Type: application/json
Content-Length: 27
Authorization: Bearer: ABC123
{
"status": "suspended"
}
The interpretation and authentication of the PATCH request depend on the implementation. Success can be indicated by
any of the successful response status codes. In this examnple, a 204 No Content is used as there’s no need to transmit
a body with additional context about the operation. An ETag is provided so the caller can perform a conditional request in the future:
HTTP/1.1 204 No Content
Content-Location: /users/122
ETag: "a789sa3s"
Anki
Links
References
MDN. “PATCH method”. Available at: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods/PATCH. (Accessed: ). ↩︎
Share :
Other Articles
- Published:
- Content-Type header
- Category: Concept
- Published:
- Message body information headers
- Category: Concept