Difference between PUT and PATCH methods for ( REST Scripted API and SOAP)

Sneha39
Mega Guru

Hi,

I am looking for an article where I can understand properly with example difference between PUT and PATCH methods for ( REST Scripted API and SOAP) ?

Thanks in advance

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

Hi Sneha,



PATCH: it is used to update an existing entity with new information. You can't patch an entity that doesn't exist. You would use this when you have a simple update to perform, e.g. changing a user's name.


partially updates the resource into the server mapped by the provided data. As an example, PATCH /api/log/1 will only update properties mapped in the request body. Just like the PUT method it may include an If-Match with the ETag and in case of success 200 Ok or 202 Accepted with the new ETag value should be returned, or in case of a failure a 404 Not Found or 409 Conflict will be indicated with the same conditions as the PUT method. This about it like a partial update to a row in a table, where only some columns are affected.





PUT: it is used to set an entity's information completely. PUTting is similar to POSTing, except that it will overwrite the entity if already exists or create it otherwise. You could use a PUT to write a user to your database that may already be in it.


replaces the existing resource by the new one. As an example, PUT /api/log/1 will completely replace that resource for the new one. Optionally in the request you may include an If-Match with the ETag value to be sure you are replacing the expected version of that resource (preventing concurrency problems). In case of success, it should return a 200 Ok or 202 Accepted (async operations) with the new ETag. In case the entity does not exist, a 404 Not Found must be returned or a 409 Conflict if the version or any other value in the entity are not correct with internal server state (like referenced links). Think about it like an update to a complete row in a table;


View solution in original post

5 REPLIES 5

tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Sneha,



as per   Pradeep Sharma


What is the difference between PUT and PATCH methods of REST Table API?


..


In the REST world, PUT and PATCH have different semantics. PUT means replace the entire resource with given data (so null out fields if they are not provided in the request), while PATCH means replace only specified fields.   For the Table API, however, PUT and PATCH mean the same thing.   PUT and PATCH modify only the fields specified in the request.


..



There are other contributions that indicate that patch is preferred however.



I recommend using PATCH for ServiceNow REST as there is no disadvantage is using it - compared to PUT - and no one will come along and say you should use PUT



Patch verb - Wikipedia  



If the reply was informational, please like, mark as helpful or mark as correct!


Harsh Vardhan
Giga Patron

Hi Sneha,



PATCH: it is used to update an existing entity with new information. You can't patch an entity that doesn't exist. You would use this when you have a simple update to perform, e.g. changing a user's name.


partially updates the resource into the server mapped by the provided data. As an example, PATCH /api/log/1 will only update properties mapped in the request body. Just like the PUT method it may include an If-Match with the ETag and in case of success 200 Ok or 202 Accepted with the new ETag value should be returned, or in case of a failure a 404 Not Found or 409 Conflict will be indicated with the same conditions as the PUT method. This about it like a partial update to a row in a table, where only some columns are affected.





PUT: it is used to set an entity's information completely. PUTting is similar to POSTing, except that it will overwrite the entity if already exists or create it otherwise. You could use a PUT to write a user to your database that may already be in it.


replaces the existing resource by the new one. As an example, PUT /api/log/1 will completely replace that resource for the new one. Optionally in the request you may include an If-Match with the ETag value to be sure you are replacing the expected version of that resource (preventing concurrency problems). In case of success, it should return a 200 Ok or 202 Accepted (async operations) with the new ETag. In case the entity does not exist, a 404 Not Found must be returned or a 409 Conflict if the version or any other value in the entity are not correct with internal server state (like referenced links). Think about it like an update to a complete row in a table;


Hi Harsh,


It might be worth mentioning this KB which makes some observations specific to ServiceNow


ServiceNow KB: Table API FAQs (KB0534905)


11.


What is the difference between PUT and PATCH?


In the REST world, PUT and PATCH have different semantics. PUT means replace the entire resource with given data (so null out fields if they are not provided in the request), while PATCH means replace only specified fields.   For the Table API, however, PUT and PATCH mean the same thing.   PUT and PATCH modify only the fields specified in the request.



If the reply was informational, please like, mark as helpful or mark as correct!