+ Start a Discussion
Surendra nuneSurendra nune 

Clear Difference between the annotations

What is the exact difference between the following annotations?
  • HttpPut
  • HttpPatch
  • HttpPost
Please explain with examples
Best Answer chosen by Surendra nune
SantoshChitalkarSantoshChitalkar
Hi Surendra,

POST sends some data to a specified URL. The server on the other end of this URL can do whatever it wants with this data. It can store it somewhere private. (HTTP 204 NO CONTENT). It can store it in the page at the URL that was POSTed to (HTTP 205 RESET CONTENT). It can store it in a new page, in which case it returns the URL of that page in the Location field of the HTTP response header (HTTP 201 CREATED). It can use it as input for several different existing and new pages. It can throw the information away. It can insert, update, or delete records in a database (or all of the above). It can start brewing coffee (HTTP 202 ACCEPTED). It can start global thermonuclear war. POST is decidely non-side-effect free and non-idempotent.

PUT is a much more limited operation that never does anything more than PUT one page at a specified URL. It is idempotent, which is a fancy way of saying that doing it twice is the same as doing it once. Both PUT and POST can be used to create new pages. However PUT should be used when the client specifies the location for the page. PUT is normally the right protocol for a web editor like DreamWeaver or BBEdit. POST is used when the client gives sends the page to the the server, and the server then tells the client where it put it. POST is normally the right protocol for a blog editor like TypePad or anything that inputs into a content management system. In SQL analogy, POST is an INSERT with an automatically generated primary key, and PUT is an INSERT that specifies the primary key in the INSERT statement.

The HTTP methods PATCH can be used to update partial resources. For instance, when you only need to update one field of the resource, PUTting a complete resource representation might be cumbersome and utilizes more bandwidth.

Let us know if this helps you.

Regards,

Santosh Chitalkar

All Answers

Anupam RastogiAnupam Rastogi
Hi Surendra,

All these three annotations are used at the Method level and enables us to expose an Apex method as a REST resource. And here are the differences - 
  • HttpPut - Used for Create and Update
  • HttpPatch - Used for Update
  • HttpPost - Used for Create
So if you only wish to create a new record then you can use HttpPost. If you wish to first check if a record exists and only when it does not exists then create a new record, use HttpPut. And if you are sure that the record exists and in case it does not then you wish to throw error then use HttpPatch.

Thanks
AR

If you found the reply useful that solved your problem then please mark it as best answer.
Surendra nuneSurendra nune
Thanks for the quick reply Anupam.

Why do we require 3 different annotations when we can acheive all the above mentioned usecases with one annotation HttpPost?
SantoshChitalkarSantoshChitalkar
Hi Surendra,

POST sends some data to a specified URL. The server on the other end of this URL can do whatever it wants with this data. It can store it somewhere private. (HTTP 204 NO CONTENT). It can store it in the page at the URL that was POSTed to (HTTP 205 RESET CONTENT). It can store it in a new page, in which case it returns the URL of that page in the Location field of the HTTP response header (HTTP 201 CREATED). It can use it as input for several different existing and new pages. It can throw the information away. It can insert, update, or delete records in a database (or all of the above). It can start brewing coffee (HTTP 202 ACCEPTED). It can start global thermonuclear war. POST is decidely non-side-effect free and non-idempotent.

PUT is a much more limited operation that never does anything more than PUT one page at a specified URL. It is idempotent, which is a fancy way of saying that doing it twice is the same as doing it once. Both PUT and POST can be used to create new pages. However PUT should be used when the client specifies the location for the page. PUT is normally the right protocol for a web editor like DreamWeaver or BBEdit. POST is used when the client gives sends the page to the the server, and the server then tells the client where it put it. POST is normally the right protocol for a blog editor like TypePad or anything that inputs into a content management system. In SQL analogy, POST is an INSERT with an automatically generated primary key, and PUT is an INSERT that specifies the primary key in the INSERT statement.

The HTTP methods PATCH can be used to update partial resources. For instance, when you only need to update one field of the resource, PUTting a complete resource representation might be cumbersome and utilizes more bandwidth.

Let us know if this helps you.

Regards,

Santosh Chitalkar
This was selected as the best answer
Surendra nuneSurendra nune
Thank you so much for the detailed explaination Santosh!