O'Reilly logo

RESTful Web Services Cookbook by Subbu Allamaraju

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.9. How to Use the PATCH Method

The HTTP PUT method is defined for the complete update or replacement of a resource. The PATCH method is designed to support partial updates. See RFC-5789 to read more about this method.

Problem

You want to know how to use the PATCH method.

Solution

The PATCH method is an unsafe and nonidempotent HTTP method. The body of the request is a representation that describes a set of changes that need to be made to the resource. On receiving this request, the server applies the changes to the resource and returns response code 200 (OK) or 204 (No Content).

Make the implementation of this method conditional by requiring clients to supply If-Unmodified-Since and/or If-Match headers. Return response code 412 (Precondition Failed) if the supplied preconditions do not match.

Advertise support for the PATCH via the Allow header of the OPTIONS response. Also include an Accept-Patch header with the supported media types for the PATCH method.

Discussion

Here is an example of a client submitting a PATCH request to modify the customer’s name, add a nickname, and remove the fax number. Except for the representation in the request, this request-response pair is similar to using a conditional PUT request.

# Request for a partial update
PATCH /customers/1 HTTP/1.1 1
Host: www.example.org
If-Match: "2a7ad61820b6ba89e6c4a119e22f7dfc"  If-Unmodified-Since: Thu, 05 Nov 2009 01:00:01 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required