HTTP 中的 POST 和 PUT 有什么区别? [英] What is the difference between POST and PUT in HTTP?
问题描述
根据 RFC 2616, § 9.5, POST
用于创建一个资源:
According to RFC 2616, § 9.5, POST
is used to create a resource:
POST 方法用于请求源服务器接受请求中包含的实体作为请求行中的请求 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
用于创建或替换一个资源:
According to RFC 2616, § 9.6, PUT
is used to create or replace a resource:
PUT 方法请求将封闭的实体存储在提供的 Request-URI 下.如果请求 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.只需确保 PUT 幂等性实际上在服务器中正确实现.
- 您可以使用具有相同对象 URL 的 PUT 更新或创建资源
- 使用 POST,您可以同时收到 2 个请求,对 URL 进行修改,它们可能会更新对象的不同部分.
示例:
我在 关于此问题的另一个答案:
发布:
用于修改和更新资源
POST /questions/<existing_question> HTTP/1.1
Host: www.example.com/
注意以下错误:
POST /questions/<new_question> HTTP/1.1
Host: www.example.com/
如果 URL 尚未创建,您不应该使用 POST 来创建它在指定名称时.这应该导致找不到资源"错误因为
不存在然而.你应该把
首先是服务器上的资源.
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.
PUT:
用于创建资源,或覆盖它.当您指定资源新网址.
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/
覆盖现有资源:
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 方法请求目标资源的状态为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屋!