Girish Mahajan (Editor)

Patch verb

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

The PATCH method is a request method supported by the HTTP protocol for making partial changes to an existing resource. The PATCH method provides an entity containing a list of changes to be applied to the resource requested using the HTTP URI. The list of changes are supplied in the form of a PATCH document. If the requested resource does not exist then the server may create the resource depending on the PATCH document media type and permissions. The changes described in the PATCH document must be semantically well defined but can have a different media type than the resource being patched. Frameworks such as XML, JSON can be used in describing the changes in the PATCH document.

Contents

History of PATCH

As per the semantics defined in the HTTP protocol, the GET, PUT, POST, DELETE methods need to use a full representation of the resource. The PUT method which can be used for resource creation or replacement is idempotent and can be used only for full updates. The edit forms used in conventional Ruby on Rails application need to create new resources by applying partial updates to a parent resource. Due to this requirement, the PATCH method was added to the HTTP protocol in 1995. The PATCH method is suitable for web programming using Ruby.

PUT vs PATCH vs POST

HTTP is the foundation of data communication for the World Wide Web. HTTP is a request-response protocol which helps users communicate with the server to perform CRUD operations. HTTP supports a number of request methods such as PUT, POST and PATCH to create or update resources.

The main difference between the PUT and PATCH method is that the PUT method uses the request URI to supply a modified version of the requested resource which replaces the original version of the resource whereas the PATCH method supplies a set of instructions to modify the resource. If the PATCH document is larger than the size of the new version of the resource sent by the PUT method then the PUT method is preferred.

The POST method can be used for sending partial updates to a resource. The main difference between the POST and the PATCH method is that the POST method can only be used when it is written to support the applications or the applications support its semantics whereas the PATCH method can be used in a generic way and does not require application support. If the outcome of using the PATCH method is not known then the POST method is preferred.

Example

A Simple PATCH request Example

PATCH /example.txt HTTP/1.1 Host: www.example.com Content-Type: application/example If-Match: "c0b42b66e" Content-Length: 120 [changes]

[changes] is the patch document containing all the changes that need to be made on the resource example.txt

Successful PATCH response to existing text file:

HTTP/1.1 200 OK Content-Location: /example.txt ETag: "c0b42b66f"

The response 200 means that the request was processed successfully.

PATCH method for Ruby

The PATCH method is used in Rails 4.0 in the form of update_attributes method. Apache, Nginx and Phusion Passenger are some of the web servers that support the PATCH method. The PATCH requests to '/users/:id' route in config/routes.rb are directed to the update action in UserController in Rails 4.0.

Trade-offs between PUT and PATCH

Using the PUT method consumes more bandwidth as compared to the PATCH method when only a few changes need to be applied to a resource. But when the PATCH method is used, it usually involves fetching the resource from the server, comparing the original and new files, creating and sending a diff file. On the server side,the server has to read the diff file and make the modifications. This involves a lot of overhead compared to the PUT method. On the other hand, the PUT method requires a GET to be performed before the PUT and it is difficult to ensure that the resource is not modified between the GET and PUT requests.

Caution

The PATCH method is not safe. It may create new resources or change resources not mentioned in the URI.

The PATCH method is not idempotent. It can be made idempotent by using a conditional request. When a client makes a conditional request to a resource, the request succeeds only if the resource has not been updated since the client last accessed that resource. This also helps in preventing corruption of the resource since some updates to a resource can only be performed starting from a certain base point.

Error handling

A PATCH request can fail if any of the following errors occur:

Malformed patch document

The server returns a 400 (Bad request) response if the PATCH document is not formatted as required.

Unsupported patch document

The server returns a 415 (Unsupported Media Type) response with an Accept-Patch response header containing supported media types when the client sends an unsupported patch document. This informs the client that the PATCH document sent by the client cannot be applied to the requested resource.

Unprocessable request

The server returns a 422 (Unprocessable Entity) response when the server understands the PATCH document but is unable to modify the requested resource either because it causes the resource to become invalid or it results in some other error state.

Resource not found

The server returns a 404 (Not Found) response when the PATCH document cannot be applied to a non-existent resource.

Conflicting state

The server returns a 409 (Conflict) response when the server cannot apply a patch for the current state of the resource.

Conflicting modification

The server returns a 412 (Precondition Failed) response when the precondition supplied by the client using the If-Match or If-Unmodified-Since header fails. If no precondition is supplied and there is a conflicting modification then the server returns a 409 (Conflict) response.

Concurrent modification

The server returns a 409 (Conflict) response if the PATCH requests to a certain resource need to be applied in a certain order and the server is not able to handle concurrent PATCH requests.

Security Considerations

The PATCH request needs to use mechanisms such as conditional requests using Etags and the If-Match request header to ensure that data is not corrupted while patching. In case of a failure of a PATCH request or failure of the channel or a timeout, the client can use a GET request to check the state of the resource. The server has to ensure that malicious clients do not use the PATCH method for consuming excessive server resources.

References

Patch verb Wikipedia