The main difference between PUT and PATCH in REST API is that PUT handles updates by replacing the entire entity, while PATCH only updates the fields that you give it.
1. PUT Method
The PUT method is used to completely replace the representation of a resource with the provided data. It requires the client to send the entire updated representation of the resource, including both the modified fields and any unchanged fields. If the resource already exists, it is replaced with the new representation sent in the request. If the resource does not exist, it may be created with the provided representation. PUT is considered an idempotent operation, meaning that multiple identical PUT requests will have the same effect as a single request.
Example
Let’s consider a simple example of registering the employee details. So, collects information about the employee and POST the first registration of a particular deposit box.
After some time, the deposit box is transferred to another Employee details. Thus, the information of the new employee and updates all the data of the deposit box through a PUT method.
It is relevant to observe that the PUT method sets up the entity with the exact information provided in the request. In this way, the request must contain the entire entity, not only specific fields. But, once the deposit box was transferred to a new employee, it is expected that all the personal information (or most of it, at least) will change. So, PUT fits well in this scenario.
2. PATCH Method
The PATCH method is used to partially update a resource with the provided data. It allows the client to send only the modified fields or properties that need to be updated, rather than sending the entire representation of the resource. The server applies the provided changes to the resource, modifying only the specified fields while leaving other fields unchanged. PATCH is not required to be idempotent. Multiple PATCH requests to the same resource with the same set of changes may result in different outcomes if the server applies the changes incrementally.
Example
let’s consider a new case of the example presented in the previous section. After some time, the new employee of the deposit box changes her e-mail address. So, she informs the new e-mail address to the deposit box agent. The agent, in turn, executes an update in the employee register through a PATCH method.
Unlike the PUT method, the PATCH method allows the data update of particular fields of an entity. In our example, the deposit box employee changed only the e-mail information, keeping the rest of the register with the same data. So, the PATCH method fits well to handle this specific update.
SNo. | HTTP PUT | HTTP PATCH |
1 | The PUT method is used to completely replace the representation of a resource with the provided data. | The PATCH method is used to partially update a resource with the provided data. |
2 | It requires the client to send the entire updated representation of the resource, including both the modified fields and any unchanged fields. If the resource already exists, it is replaced with the new representation sent in the request. | It allows the client to send only the modified fields or properties that need to be updated, rather than sending the entire representation of the resource. |
3 | If the resource does not exist, it may be created with the provided representation. | The server applies the provided changes to the resource, modifying only the specified fields while leaving other fields unchanged. |
4 | PUT is considered an idempotent operation, meaning that multiple identical PUT requests will have the same effect as a single request. | PATCH is not required to be idempotent. Multiple PATCH requests to the same resource with the same set of changes may result in different outcomes if the server applies the changes incrementally. |