asp.net mvc and web api which is better Http POST or PUT
I have an application of type asp.net mvc and web api. I m little bit confused over http post and http put. When to use what and what is the pros and cons of each. I have gone through many blogs but no solid reason what is designed for what.
Use POST
where you would have to create completely new record from scratch. Use PUT
where you would have to update existed record in your database
Here are Differences between PUT & POST
`POST is Not idempotent`-->
Means running POST operation again and again will create new instance everytime when you run call it.
`PUT is Idempotent`-->
PUT is Idempotent operation calling PUT again and again will result same result.
So POST is not idempotent while PUT is idempotent.
`There is also PATCH` -->
Use patch when you would have to update only few properties of your model.In other words Partial Updates.
Put simply (no pun intended):
POST
is usually used to CREATE new objects.
PUT
is usually used to UPDATE existing objects
Using the correct HTTP verbs allows you to publish a cleaner API and negates the need for encoding intent within the endpoint (url). For example, compare:
Using the correct verbs:
GET api/user/12345
POST api/user/12345
PUT api/user/12345
DELETE api/user/12345
Hacking the endpoint:
GET api/user/12345
POST api/user/12345/create
POST api/user/12345/update
POST api/user/12345/delete
I think the only Cons of using PUT
etc are that not all developers are familiar with them and some third party software may not support them or at least it may not be as easy as using the more familiar verbs like GET
& POST
.
For example, I had a problem a few weeks ago when a proxy was placed in front of an API just before it was to go live and the proxy didn't support the HTTP PUT
verb (maybe a config issue - but we didn't have access to the proxy to fix it) so we had to tweak the API and change it to POST
at the last minute (which also meant we had to change the clients (mobile apps) that were using it).
上一篇: 静止和放置之间的区别