简体   繁体   English

PUT 方法可以插入、删除。 那么为什么我们必须使用 POST 方法而不是 PUT/DELETE/GET 方法

[英]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 动词也有更多限制。

  • GET parameters are passed as part of the URL, which is of small and limited length of 256 chars by default, with some servers supporting 4000+ chars. GET 参数作为 URL 的一部分传递,默认情况下该 URL 的长度有限,只有 256 个字符,某些服务器支持 4000+ 个字符。 If you want to insert a long record,you'll have to use POST.如果要插入长记录,则必须使用 POST。 Post is considered limitless but there are limitations which vary according to browsers.帖子被认为是无限的,但有一些限制因浏览器而异。
  • GET is re-executed if a user presses a Back button in a browser.如果用户在浏览器中按下后退按钮,则重新执行 GET。 Post doesn't.帖子没有。
  • GET is cached by browsers, nodes in the network, Internet Service Providers. GET 被浏览器、网络中的节点、互联网服务提供商缓存。
  • Unless the content changes, GET to the same URL must return same results to all the users or else you won't have any trust what so ever in the returned result.除非内容发生变化,否则对同一 URL 的 GET 必须向所有用户返回相同的结果,否则您将无法信任返回的结果。 etc.等等。

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.

相关问题 ASP.NET Web API:“消息”:“请求的资源不支持 http 方法 'POST'、PUT、DELETE。” 在邮递员 - ASP.NET Web API: "Message": "The requested resource does not support http method 'POST', PUT, DELETE." in Postman 我应该将 _method=PUT/DELETE 存储在帖子中还是 url 中 - Should I store _method=PUT/DELETE in the post or in the url .NET框架中是否有针对不同Web方法类型(GET,PUT,POST,DELETE,HEAD)的常量? - Are there any constants in the .NET framework for the different web method types (GET, PUT, POST, DELETE, HEAD)? WCF RESTful 应用程序 PUT、POST、DELETE 收到相同的错误:“该方法不被允许”。 - WCF restful application PUT, POST, DELETE receive the same error: "the method is not allowed." 使用相同的方法进行PUT和POST - Using the same method to PUT and POST 在Web Api中使用“放置”或“删除”方法时,我必须避免405方法不允许错误吗? - What alternative do I have to avoid 405 Method Not Allowed error when using Put or Delete method in Web Api? 使用WebClient调用Web API方法(PUT,DELETE) - Invoking Web API method(PUT,DELETE) using an WebClient Elastcsearch NEST 客户端始终使用 PUT http 方法而不是 POST - Elastcsearch NEST client is always using PUT http method instead of POST 在SQL Server中插入新数据的发布或放置方法? - Post or Put method for insert new data in SQL Server? 为什么控制器方法充当POST而不是GET? - Why a controller method is acting as POST instead GET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM