I just wanted to add something to the accepted answer because his definition of the http verbs
are incorrect. They all have a spec which "should" be followed and you can create/update/delete with multiple http verbs
based on the specs.
I am going to highlight some of the important bits in the RFC 2616 by W3
I'm going to start with PUT
because in my opinion it has the most confusion surrounding it.
PUT is used for both create/update PUT updates by completely replacing the resource on the server with the resource sent in the request
For example
You make this call to my api
PUT /api/person
{
Name: John,
email: jdoe@hra.com
}
my Server has this resource living on the server
{
Name: Jane,
email: jdoe@hra.com
}
Now my existing resource is completely replaced by what you sent over and this is what I have on my server.
{
Name: John,
email: jdoe@hra.com
}
So if you PUT
and only send an email in the body
PUT /api/person
{
email: jdoe@hra.com
}
My Server will completely replace the entity
{
Name: Jane,
email: jdoe@hra.com
}
With
{
email: jdoe@hra.com
}
And Name will be gone. Partial updates are for PATCH
but I use POST
for that anyway.
One of the main reasons why we create/update with put is because it is idempotent.
It's just a fancy term and the basic definition of it is multiple identical requests are the same for a single request.
Example
Suppose I PUT
a file to api/file
if the origin server does not find that file it will create one. If it does find a file it will completely replace the old file with the one I sent over. This ensures that one file is ever created and updated. If no file exists and you call PUT
5 times, the first time it creates a file then the other 4 times it replaces the file with what you send over. If you call a POST
5 times to create it will create 5 files.
You PUT to that exact URI. If you don't you have to send a 301 (Moved Permanently) to the user and allow then make a choice whether or not to redirect the request. Most times the server you PUT to usually hosts the resource and takes care of updating it
Those are the major points in when to use PUT
As far as POST
is concerned
You can also create/update and then some...
As I mentioned above there are a few key differences.
- Post is more General. In what ways? some other examples include a gateway to other protocols, it could take the response and send it to some data handler out in the middle of yonder, or it can extend some sort of functionality.
- Post doesn't have the restriction of "To the exact URI or notifiy" for example
POST
can append a resource to an existing collection and decide where it's stored.
Now what about Delete
Why don't I just POST
?
When you DELETE
, the server SHOULD NOT respond with success unless you delete the resource or move it to an inaccessible location at the time the response is sent.
Why is that important? What if you call DELETE
but the resource has to go through "APPROVAL" before being deleted? If the delete can be rejected you can't send a successful error code and if you do follow the basic specs on this it's confusing to the caller. Just an example I'm sure you can think of many others.
I just highlighted some of the major points on when to use the common Http verbs