Difference between PUT and POST
The key difference between
PUT
and POST
is that PUT
is idempotent while POST
is not. No matter how many times you send a PUT
request, the results will be same. POST
is not an idempotent method. Making a POST
multiple times may result in multiple resources getting created on the server.
Another difference is that, with
PUT
, you must always specify the complete URI of the resource. This implies that the client should be able to construct the URI of a resource even if it does not yet exist on the server. This is possible when it is the client's job to choose a unique name or ID for the resource, just like creating a user on the server requires the client to choose a user ID. If a client is not able to guess the complete URI of the resource, then you have no option but to use POST
.
Request
|
Operation
|
PUT http://MyService/Persons/ |
Won't work.
PUT requires a complete URI |
PUT http://MyService/Persons/1 |
Insert a new person with
PersonID=1 if it does not already exist, or else update the existing resource |
POST http://MyService/Persons/ |
Insert a new person every time this request is made and generate a new
PersonID . |
POST http://MyService/Persons/1 |
Update the existing person where
PersonID=1 |
It is clear from the above table that a
PUT
request will not modify or create more than one resource no matter how many times it is fired (if the URI is same). There is no difference between PUT
and POST
if the resource already exists, both update the existing resource. The third request (POST http://MyService/Persons/
) will create a resource each time it is fired.
No comments:
Post a Comment