PUT vs PATCH

PUT vs PATCH

Knowledge Transfer - NODE JS

Introduction

Put and Patch are both HTTP methods and basically, both are used to update or modify the data on the web server. While some people may think the methods are the same because the task methods are performing is the same, and yet there are some major differences between these two methods.

PUT Method

The PUT method is used to completely replace an existing resource with a new representation. It requires the client to send a complete representation of the resource in the request payload. When the server receives a PUT request, it replaces the entire resource at the specified URL with the new representation provided in the request payload. If the resource does not exist at the given URL, it may create a new resource.

PATCH Method

The PATCH method is used to partially update an existing resource. It allows the client to send only the changes or differences between the current representation of the resource and the desired updated representation. The server applies these changes to the resource, modifying only the specified fields or properties, while leaving the rest of the resource unchanged.

More Detailed View

PUT MethodPATCH Method
PurposeCompletely replaces an existing resourcePartially updates an existing resource
RequestRequires the client to send a completeAllows the client to send only the changes or
PayloadRepresentation of the resourceDifferences between the current representation
ResourceReplaces the entire resource at theApplies changes to the specified fields or
Error HandlingMay result in unintended consequences ifTypically safer as it avoids overwriting

Ending Note

Please keep in mind that the actual behavior and implementation of these methods can vary depending on the server or API framework being used. The above content represents the general differences and behavior of the PUT and PATCH methods.

Did you find this article valuable?

Support Sandeep Rana by becoming a sponsor. Any amount is appreciated!