PUT vs PATCH
PUT
is used to completely replace an existing resource with a new one. When a PUT
request is made, the entire resource is replaced with the new representation provided in the request payload. A PUT
request is idempotent, which means that making the same request multiple times should have the same effect as making it once. This is because the same resource is replaced with the same representation each time the request is made.
PATCH
, on the other hand, is used to make a partial update to an existing resource. When a PATCH
request is made, only the fields specified in the request payload are updated. PATCH
in HTTP is not necessarily idempotent because the effect of a PATCH
request can depend on the current state of the resource being updated.
For example, consider a PATCH
request to increment a counter field in a resource. If the current value of the counter is 5, and the request body instructs to increment the counter by 1, the new value will be 6. If the same PATCH
request is made again, the value of the counter will be incremented to 7. Therefore, the effect of the PATCH
request is not the same each time it is made, and the request is not idempotent.