PATCH
requests describe a set of operations to be applied to a resource, if you apply the same set of operations twice to the same resource, the result may not be the same. This is because defining the operations is up to you. In other words you have to define the merging rules.
Remember a PATCH
request could be used for patching resources in many different formats, not just JSON.
So a PATCH
request can be idempotent if you define the merging rules to be idempotent.
Idempotent example:
// Original resource
{
name: 'Tito',
age: 32
}
// PATCH request
{
age: 33
}
// New resource
{
name: 'Tito',
age: 33
}
Non-idempotent example:
// Original resource
{
name: 'Tito',
age: 32
}
// PATCH request
{
$increment: 'age'
}
// New resource
{
name: 'Tito',
age: 33
}
In the second example I used a "Mongo like" syntax I made up for incrementing an attribute.
Clearly this is not idempotent, as sending the same request multiple times would result in different results each time.
Now you may be wondering if using such a made up syntax is valid. According to standards, it is:
The difference between the PUT and PATCH requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI. In a PUT request, the enclosed entity is considered to be a modified version of the resource stored on the origin server, and the client is requesting that the stored version be replaced. With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version.
And you may also be wondering whether it is restful to use PATCH
requests this way, and many people consider it is not, here's a good answer with lots of comments about the issue.
So, according to standards, it is not idempotent (unless you want it to be). Making it idempotent is definitely restful. Whether it is restful keeping it not idempotent that's debatable.