gpt4 book ai didi

http - PUT 和 DELETE HTTP 请求方法有什么用?

转载 作者:可可西里 更新时间:2023-11-01 15:03:32 24 4
gpt4 key购买 nike

我从未使用过 PUT 或 DELETE HTTP 请求方法。我倾向于在系统状态(我的应用程序或网站)可能不受影响(如产品列表)时使用 GET,在它受到影响时使用 POST(如下订单)。这两个总是不够用,还是我遗漏了什么?

最佳答案

DELETE用于删除请求资源:

The DELETE method requests that the origin server delete the resource identified by the Request-URI. This method MAY be overridden by human intervention (or other means) on the origin server. The client cannot be guaranteed that the operation has been carried out, even if the status code returned from the origin server indicates that the action has been completed successfully …

PUT 用于在服务器上放置或更新资源:

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI …

完整规范请访问:

Since current browsers unfortunately do not support any other verbs than POST and GET in HTML forms ,您通常无法对他们充分利用 HTTP(尽管您仍然可以通过 JavaScript 劫持他们的提交)。 HTML 表单中不支持这些方法导致 URI 包含动词,例如

POST http://example.com/order/1/delete

甚至更糟

POST http://example.com/deleteOrder/id/1

通过 HTTP 有效地隧道化 CRUD 语义。但是动词从来都不是 URI 的一部分。相反,HTTP 已经通过 HTTP 方法为资源(例如订单)提供了 CRUD 机制和语义。 HTTP 是一种协议(protocol),而不仅仅是一些数据隧道服务。

所以要删除网络服务器上的资源,您可以调用

DELETE http://example.com/order/1

要更新它你会调用

PUT http://example.com/order/1

并在 PUT 正文中提供更新后的资源表示供网络服务器应用。

因此,如果您正在为 REST API 构建某种客户端,您可能会让它发送 PUT 和 DELETE 请求。这可能是内置于浏览器中的客户端,例如通过 JavaScript 发送请求,或者它可以是在服务器上运行的某种工具等。

有关更多详细信息,请访问:

关于http - PUT 和 DELETE HTTP 请求方法有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12142652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com