PATCH

Last Updated Jul 06, 2021

What is PATCH?  

The REST (REpresentational State Transfer) protocol was designed to standardize HTTP communication between clients and servers. REST is a system of rules and connectors all services must follow and use. If you’ve ever had to order a special dongle for that one piece of hardware without a USB connector, you understand the problem that REST solves.

REST APIs perform specific methods of data operations across HTTP:

  • GET - This operation reads information from a record in the database.
  • PUT- This operation changes a record’s information in the database.
  • POST - This operation creates a new record in the database.
  • PATCH - This operation updates an existing resource, but does not require sending the entire body with the request.
  • DELETE - This operation removes a record from the database.  

In this article we are focusing on the PATCH operation. What does PATCH do, and how is it different from POST and PUT? In the [words of the RFC2028 memo, "The PATCH method is similar to PUT except that the entity contains a list of differences between the original version of the resource identified by the Request-URI and the desired content of the resource after the PATCH action has been applied."

PATCH API Example

A PATCH request sends data to an API to update an existing resource. PATCH is like a single field in a contact form on a website. When you fill out a single field and hit Send, that data is put in the response body of the request and sent to the server. This may be JSON, XML, or query parameters.

A PATCH request might look like this: `PATCH https://www.abstractapi.com/users/{{userID}}`. This would send data to the `/users/{{userID}}` endpoint, and update that user's information.  

A successful PATCH request should return a `2xx` status code, and PATCH requests should fail if invalid data is supplied in the [request](https://restfulapi.net/http-methods/).

What is the Difference between PUT, PATCH, and POST?

Maybe you looked at the list of operations above and wondered what the differences are between POST, PUT, and PATCH. We use them for different situations, depending on idempotency. If a request is idempotent, calling it once or several times successively has the same effect. If the request is non-idempotent, each successive request will act on the previous request, causing potential problems.  

How is PATCH different from PUT?

The difference between PATCH and PUT is that PUT is idempotent: calling it once or several times successively has the same effect, whereas successive identical PATCH requests may have additional effects, akin to placing an order several times.  

How is PATCH Different from POST?

PATCH is used less frequently than POST. PATCH applies only partial modification to a resource, unlike POST and PUT, which modify the entire resource. PATCH is non-idempotent, while PUT is idempotent.  

Using the `PATCH https://www.abstractapi.com/users/{{userID}}` example from above, if a user wanted to change their username, a POST or PUT request would send the entire user entity, while PATCH could send just the updated username.  

Conclusion

Becoming confident in request methods is an important step in your programming journey. They might seem a little confusing in the beginning, but knowing when to use a POST, a PUT, or a PATCH will make you use resources more efficiently. You might not need to understand the deep mathematical complexities of idempotency, but it's important to know how you are modifying a record with POST, PUT, or PATCH.

Try Abstract's suite of free API's,
trusted by 100,000+ developers.
Get started