PATCH

The HTTP PATCH method is a type of HTTP request that is used to modify a resource on the server.

Table of Contents

Usage

When the HTTP PATCH method is called, the client instructs the server to make changes to the specified resource. If the resource does not exist then it can be created, depending on the server configuration.

Although similar to an HTTP PUT request, it differs significantly. An HTTP PATCH request modifies a resource, whereas an HTTP PUT request replaces it entirely.

The server can respond to an HTTP PATCH request in several ways. If the HTTP request is successful then it might return a 200 OK or 204 No Content HTTP status code. However, the server will return 415 Unsupported Media Type if the client sends a document format that is not supported by the server for the target resource. Also, if the resource has been modified recently then it might be in a conflicted state, at which point the server will return 409 Conflict or perhaps 412 Precondition Failed if it was able to recognize that the resource is no longer exactly the one specified by the client.

Not all servers support the HTTP PATCH method for different types of resources. The Accept-Patch HTTP header will specify which document formats are accepted.

Importantly, a server will apply all of the patches atomically. Success in this method is all or nothing, meaning that the server will never store a new representation that is only altered in part. If any of the specified patches fail then the entire operation is unsuccessful.

Note

An HTTP PATCH request can have side effects on resources in the same way that an HTTP POST request might.

Example

In this example, the client sends instructions for modifying an HTML document that is stored on the server. The HTTP PATCH method may have been chosen rather than using an HTTP PUT because it is a lengthy, living document that is regularly changed, yet not stored, by several people.

Request

PATCH /livingdoc.html HTTP/1.1
Host: www.example.re
If-unmodified-since: 01 January 2021 12:00:00 GMT
Content-Type: text/html
Content-Length: 75

<instructions for modifying the livingdoc.html file>

Response

HTTP/1.1 204 No Content

Takeaway

The HTTP PATCH request method is used to modify a resource on the server, or perhaps create it, but not replace it entirely. It is not safe and not guaranteed to be idempotent, but the result may be cacheable.

Last updated: August 2, 2023