Put 与 Post - REST
在查看 Spring 3.0 示例的一部分“petclinic”中的代码时,我注意到以下几行在
<c:choose>
<c:when test="${owner.new}"><c:set var="method" value="post"/></c:when>
<c:otherwise><c:set var="method" value="put"/></c:otherwise>
</c:choose>
SO 看来 PUT 应该用于“创建/更新”,POST 应该用于“更新”。
哪个是对的?
使用 post 进行“创建”并使用 put 进行“更新”有何影响?
注意:根据 HTTP/1.1 规范。在引用的 SO 讨论中引用,上面给出的代码似乎具有正确的行为。
While looking at the code in "petclinic", part of Spring 3.0 samples I noticed the following lines
<c:choose>
<c:when test="${owner.new}"><c:set var="method" value="post"/></c:when>
<c:otherwise><c:set var="method" value="put"/></c:otherwise>
</c:choose>
In this discussion at SO it seems that PUT should be used for "create/update" and POST for "updates".
Which is right?
What is the impact of using post for "create" and put for "update"?
Note : According to the HTTP/1.1 spec. quoted in the referenced SO discussion, the code given above seems to have the correct behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 HTTP 规范,POST 和 PUT 都有明确定义的行为。
POST请求的结果应该是一个从属于请求URL的新资源;响应应包含带有新创建资源的 URL 的 Location 标头。
PUT 的结果应该是请求 URL 处资源的更新。如果请求 URL 处不存在现有资源,则可以创建一个新资源。
造成混乱的原因是 POST 也与表单一起使用作为传递表单数据的机制。最常见的表单实现是回发到表单页面所在的同一 URL,从而产生 POST 操作用于更新的错误想法。然而,在这个特定的用法中,表单页面不是资源。
考虑到所有这些,这是正确的(当然是我认为的:-))用法:
在以下情况下应该使用 POST 来创建新资源:
- 新资源从属于现有资源
- 在创建时资源身份/URL 未知。
应使用 PUT 来更新具有已知 URL 的现有资源。它也可用于在众所周知的 URL 处创建资源;但是,以不同的方式考虑这种情况确实有帮助 - 如果在发出 PUT 请求之前已知资源 URL,则可以将其视为与该位置处已存在但为空的资源相同。
Both POST and PUT are have well defined behavior as per HTTP spec.
The result of a POST request should be a new resource that is subordinate to the request URL; the response should contain Location header with the URL of the newly created resource.
The result of a PUT should be an update of the resource at the request URL. if there is no existing resource at the request URL, a new one can be created.
The confusion arises from the fact that POST is also used with forms as a mechanism to pass the form data. Most common implementation of forms is to post back to the same URL at which the form page is located, thus giving the false idea that the POST operation is used for an update. However, in this particular usage, the form page is not the resource.
With all this in mind, here's the correct (in my opinion of course :-)) usage:
POST should be used to create new resources when:
- the new resource is subordinate to an existing resource
- the resource identity/URL is not known at creation time
PUT should be used to update existing resources with well-known URL. It can be used to create a resource at well-known URL as well; however, it does help to think about this scenario in a different way - if the resource URL is known before the PUT request is made, this could be treated the same as the resource at this location already existing but being empty.
这非常简单:
POST
允许任何事情发生,并且它不限于创建“从属”资源,而是允许客户端“提供数据块...... . 到数据处理过程” (RFC 2616 秒 9.5)。POST
的意思是“这是您刚才请求的数据”PUT
与GET
相反。通常的流程是,您GET
一个资源,以某种方式修改它,然后将它放回您从中获取它的同一 URI。PUT
表示“请将此文件存储在此 URI 中”。PUT
(用于存储文件)的统一性允许中介(例如缓存)使任何缓存的响应无效他们可能在该确切的 URI 上(因为他们知道它是即将改变)。PUT
的统一性还允许客户端(理解这一点)通过首先检索资源 (GET
) 来修改资源,然后发回修改后的副本(PUT
)。由于PUT
的幂等性,它还允许客户端在网络故障时重试。旁注:使用
PUT
创建资源是值得怀疑的。虽然在规范中这是可能的,但我不认为这是一个好主意,就像使用 POST 执行搜索不是一个好主意一样,就像通过 HTTP 建立 SOAP 隧道也不是一个好主意一样。 AtomPub 明确指出 PUT 不用于创建原子条目。POST
的普遍存在源于这样一个事实:HTML 定义了201
响应和Location
标头)201
响应和Location
标头)200
或302
)(可能应该使用 GET 的情况)唯一知道
POST
请求中会发生什么的人是发起请求的用户(通过单击巨大的“是的,我确认删除我的 Facebook 个人资料”按钮)和处理该请求的服务器。对于世界其他地方来说,该请求是不透明的,除了“此 URI 正在传递一些数据”之外没有任何意义。因此,您问题的答案是
POST
和PUT
都可以用于创建和更新。It's quite simple:
POST
allows anything to happen, and it isn't restricted to creating "subordinate" resources, but allows the client to "provide a block of data ... to a data-handling process" (RFC 2616 sec 9.5).POST
means "Here's that data you asked for just now"PUT
is used as an opposite ofGET
. The usual flow is that youGET
a resource, modify it somehow, and then you PUT it back at the same URI that you got it from.PUT
means "Please store this file at this URI".The uniformity of
PUT
(which is to store a file) allows intermediaries (e.g. caches) to invalidate any cached responses they might have at that exact URI (since they know that it's about to change). The uniformity ofPUT
also allows clients (that understand this) to modify a resource by first retrieving it (GET
) and then send a modified copy back (PUT
). It also allows clients to retry on a network failure, due toPUT
's idempotency.Side note: Using
PUT
to create resources is dubious. While it's possible within the spec, I don't see it as a good idea, just as using POST to perform searches isn't a good idea, just as tunneling SOAP over HTTP isn't a good idea. AtomPub explicitly states that PUT isn't used to create atom entries.POST
s ubiquitousness comes from the fact that HTML defines<form>
elements that result in POSTing aapplication/x-www-form-urlencoded
entity, with which the recipient can do anything it pleases, including201
response andLocation
header)201
response andLocation
header)200
or302
) (a case where perhaps GET should have been used)The only one who knows what will happen in a
POST
request is the user who initiated the request (by clicking the huge "yes I confirm deleting my Facebook profile" button) and the server that's handling the request. To the rest of the world, the request is opaque and doesn't mean anything other than "this URI is being passed some data".So the answer to your question is that both
POST
andPUT
can be used for both create and update.