http - HTTP 中的 POST 和 PUT 有什么区别?

标签 http rest post put

背景信息分析:

根据 RFC 2616, § 9.5 , POST 用于创建资源:

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

根据 RFC 2616, § 9.6 , 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.

我的问题:

那么,应该使用哪种 HTTP 方法来创建资源呢?还是应该同时支持两者?

最佳答案

总体:

PUT 和 POST 都可以用于创建。

您必须问“您要根据什么执行操作?”,以区分您应该使用什么。假设您正在设计一个用于提问的 API。如果您想使用 POST,那么您可以对问题列表执行此操作。如果您想使用 PUT,那么您可以针对特定问题使用 PUT。

很好,两者都可以使用,所以我应该在我的 RESTful 设计中使用哪一个:

您不需要同时支持 PUT 和 POST。

你使用哪个取决于你。但请记住根据您在请求中引用的对象使用正确的对象。

一些注意事项:

  • 您是明确命名您创建的 URL 对象,还是让服务器决定?如果你给它们命名然后使用 PUT。如果您让服务器决定,则使用 POST。
  • PUT 被定义为假定幂等性,因此如果您将一个对象 PUT 两次,它应该没有额外的效果。这是一个很好的属性,所以我会尽可能使用 PUT。只需确保 PUT 幂等性实际上已在服务器中正确实现。
  • 您可以使用具有相同对象 URL 的 PUT 更新或创建资源
  • 使用 POST,您可以同时收到 2 个请求来修改 URL,它们可能会更新对象的不同部分。

一个例子:

我在 another answer on SO regarding this 中写了以下内容:

POST:

Used to modify and update a resource

POST /questions/<existing_question> HTTP/1.1
Host: www.example.com/

Note that the following is an error:

POST /questions/<new_question> HTTP/1.1
Host: www.example.com/

If the URL is not yet created, you should not be using POST to create it while specifying the name. This should result in a 'resource not found' error because <new_question> does not exist yet. You should PUT the <new_question> resource on the server first.

You could though do something like this to create a resources using POST:

POST /questions HTTP/1.1
Host: www.example.com/

Note that in this case the resource name is not specified, the new objects URL path would be returned to you.

PUT:

Used to create a resource, or overwrite it. While you specify the resources new URL.

For a new resource:

PUT /questions/<new_question> HTTP/1.1
Host: www.example.com/

To overwrite an existing resource:

PUT /questions/<existing_question> HTTP/1.1
Host: www.example.com/

此外,更简洁一点,RFC 7231 Section 4.3.4 PUT状态(添加了重点),

4.3.4. PUT

The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload.

关于http - HTTP 中的 POST 和 PUT 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37211876/

相关文章:

Java发送post登录请求

javascript - 包括 jQuery/Ajax 函数的成功/失败

http - 为什么 URL 和查询字符串部分的编码不同?

http - 是否有一个表格显示浏览器对 HTTP 方法的支持

mysql - 如何在 RESTful API 中从用户的电子邮件生成唯一的 UID?

xml - Spring XML 绑定(bind)

android - 发布到 Facebook 墙时出错

ruby - HTTP协议(protocol): How does one figure out where a site's homepage is located

http - 监听大多数端口时出现 Node.js EACCES 错误

rest - 如何在 Postman 中一次更改多个 URL?