HTTP中的POST和PUT有什么区别? [英] What is the difference between POST and PUT in HTTP?

查看:104
本文介绍了HTTP中的POST和PUT有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

POST方法用于请求源服务器接受请求中包含的实体作为请求行中Request-URI标识的资源的新下属.

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 用于创建或替换资源:

PUT方法请求将包含的实体存储在提供的Request-URI下.如果Request-URI引用了已经存在的资源,则应将封闭的实体视为原始服务器上的资源的修改版本.如果Request-URI没有指向现有资源,并且请求用户代理能够将该URI定义为新资源,则原始服务器可以使用该URI创建资源.

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方法来创建资源?还是应该两者都支持?

So which HTTP method should be used to create a resource? Or should both be supported?

推荐答案

总体:

PUT和POST均可用于创建.

Both PUT and POST can be used for creating.

您必须问您要对动作执行什么操作?",以区分您应使用的内容.假设您正在设计一个用于询问问题的API.如果要使用POST,则可以对一系列问题进行处理.如果您想使用PUT,则可以针对特定问题进行操作.

You have to ask, "what are you performing the action upon?", to distinguish what you should be using. Let's assume you're designing an API for asking questions. If you want to use POST, then you would do that to a list of questions. If you want to use PUT, then you would do that to a particular question.

好极了,两者都可以使用,因此在我的RESTful设计中应该使用哪一个:

您无需同时支持PUT和POST.

You do not need to support both PUT and POST.

您要使用哪种取决于您.但是请记住,请根据您在请求中引用的对象来使用正确的对象.

Which you use is up to you. But just remember to use the right one depending on what object you are referencing in the request.

一些注意事项:

  • 您是否命名您显式创建的URL对象,还是由服务器决定?如果您命名它们,则使用PUT.如果让服务器决定,则使用POST.
  • PUT被定义为具有幂等性,因此,如果将对象两次放置,则不会产生任何其他影响.这是一个很好的属性,因此我会尽可能使用PUT.只需确保PUT幂等性实际上已在服务器中正确实现即可.
  • 您可以使用具有相同对象URL的PUT更新或创建资源
  • 使用POST,您可以同时收到2个请求,以对URL进行修改,它们可能会更新对象的不同部分.

示例:

我将以下内容作为

POST:

用于修改和更新资源

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/

如果尚未创建URL,则您不应该使用POST来创建它在指定名称时.这应该导致找不到资源"错误因为< new_question> 不存在然而.您应该将< new_question> 服务器上的资源.

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.

您可以执行类似的操作使用POST创建资源:

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

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

请注意,在这种情况下,资源名称未指定,新对象URL路径将返回给您.

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

输入:

用于创建资源,或者覆盖它.当您指定资源新的网址.

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

对于新资源:

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第4.3.4节PUT 状态(添加了强调),

Additionally, and a bit more concisely, RFC 7231 Section 4.3.4 PUT states (emphasis added),

4.3.4.PUT

PUT方法要求目标资源的状态为 created replaced ,其状态由表示形式定义包含在请求消息有效负载中.

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中的POST和PUT有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆