[英]PUT method can insert, delete. then Why we have to use POST method instead of PUT/DELETE/GET method
A value can be passed as well retrieved using any of the GET, POST, PUT, DELETE - HTTP verbs.也可以使用任何 GET、POST、PUT、DELETE - HTTP 动词来传递和检索值。 We can even update or delete the data using GET request then why should we use DELETE for deleting a resource?我们甚至可以使用 GET 请求更新或删除数据,那为什么要使用 DELETE 来删除资源呢?
Also, What's the significance of using a POST method If a PUT method can CREATE as well as UPDATE the record?另外,如果 PUT 方法可以创建和更新记录,那么使用 POST 方法有什么意义?
You can practically use a POST to do all the CRUD operations but you shouldn't .您实际上可以使用 POST 来执行所有 CRUD 操作,但您不应该这样做。 Because Each HTTP verb has its own responsibility.因为每个 HTTP 动词都有自己的职责。
A GET is supposed to be idempotent and safe. GET 应该是幂等且安全的。 A GET can be cached and in a browser, refreshed again and again. GET 可以被缓存并在浏览器中一次又一次地刷新。 Ideally, firing a GET request shouldn't change the data.理想情况下,触发 GET 请求不应更改数据。
If HttpGet method can delete, can insert, can update and also can return value then why we use HttpPort, HttpPut and HttpDelete?如果 HttpGet 方法可以删除,可以插入,可以更新,还可以返回值,那我们为什么要使用 HttpPort、HttpPut 和 HttpDelete?
Imagine a scenario.想象一个场景。
You have a Web API method which adds a new entry to the database using GET.您有一个使用 GET 向数据库添加新条目的 Web API 方法。 If your GET request becomes a link and it gets crawled by a search engine.如果您的 GET 请求变成一个链接并且被搜索引擎抓取。 You will have your database full of duplicate data.您将拥有充满重复数据的数据库。 So it makes sense to use POST in this case.所以在这种情况下使用 POST 是有意义的。
Selecting The Appropriate Method选择合适的方法
GET - requests data from the resource and should not produce any side effect. GET - 从资源请求数据,不应产生任何副作用。
POST - method requests the server to create a resource in the database, mostly when a web form is submitted. POST - 方法请求服务器在数据库中创建资源,主要是在提交 Web 表单时。 Post is non-idempotent which means multiple requests will have different effects. Post是非幂等的,这意味着多个请求会产生不同的效果。
PUT - method requests the server to update a resource or create the resource, if it doesn't exist. PUT - 方法请求服务器更新资源或创建资源(如果资源不存在)。 Put is idempotent which means multiple requests will have the same effects. Put 是幂等的,这意味着多个请求将具有相同的效果。
DELETE - method requests that the resources, or its instance, should be removed from the database. DELETE - 方法请求应从数据库中删除资源或其实例。
POST vs PUT POST 与 PUT
POST and PUT are very similar in that they both send data to the server that the server will need to store somewhere. POST 和 PUT 非常相似,因为它们都将数据发送到服务器需要存储在某处的服务器。 if you make the same request twice using PUT, with the same parameters both times, the second request will have no effect.如果您使用 PUT 两次发出相同的请求,并且两次使用相同的参数,则第二次请求将无效。 This is why PUT is generally used for the Update scenario.这就是为什么 PUT 通常用于 Update 场景。
There are some more limitations for different Http verbs as well.不同的 Http 动词也有更多限制。
Technically the POST method covers all your use cases.从技术上讲,POST 方法涵盖了您的所有用例。 Looking at the HTTP request header:查看 HTTP 请求头:
POST /service.aspx HTTP/1.1
Host: test.com&page=2
field1=value1&field2=value2
"field1" and "field2" are POST parameters, "page" is a GET parameter even though it's a POST request. "field1" 和 "field2" 是 POST 参数,"page" 是一个 GET 参数,即使它是一个 POST 请求。
Not every server supports other methods.并非每个服务器都支持其他方法。 The reason for PUT and DELETE is because, semantically, people wanted the web request counterparts of a CRUD.使用 PUT 和 DELETE 的原因是,从语义上讲,人们想要 CRUD 的 Web 请求对应物。 What's the difference between a request that creates a new record, or just one that modifies an existing record?创建新记录的请求或仅修改现有记录的请求有什么区别? What's the difference between a reading request and one that modifies a record?读取请求和修改记录有什么区别?
The convention is to map the following:约定是映射以下内容:
C - New Record - POST
R - Read Record - GET
U - Update Record - PUT
D - Delete Record - DELETE
The actual thing is that you should follow the protocol.实际情况是您应该遵循协议。 Because, other developers have no idea what your function would do, unless the verb is specified.因为,除非指定动词,否则其他开发人员不知道您的函数会做什么。 It is just to remove the ambiguity of the API of your application.这只是为了消除您的应用程序的 API 的歧义。 Otherwise, you can surely do anything.否则,您肯定可以做任何事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.