Put 与 Post - REST

发布于 2024-09-18 04:12:36 字数 530 浏览 1 评论 0原文

在查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上青苔 2024-09-25 04:12:36

根据 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.

給妳壹絲溫柔 2024-09-25 04:12:36

这非常简单:

  • POST 允许任何事情发生,并且它不限于创建“从属”资源,而是允许客户端“提供数据块...... . 到数据处理过程” (RFC 2616 秒 9.5)。 POST 的意思是“这是您刚才请求的数据”
  • PUTGET 相反。通常的流程是,您 GET 一个资源,以某种方式修改它,然后将它放回您从中获取它的同一 URI。 PUT 表示“请将此文件存储在此 URI 中”。

PUT(用于存储文件)的统一性允许中介(例如缓存)使任何缓存的响应无效他们可能在该确切的 URI 上(因为他们知道它是即将改变)。 PUT 的统一性还允许客户端(理解这一点)通过首先检索资源 (GET) 来修改资源,然后发回修改后的副本PUT)。由于 PUT 的幂等性,它还允许客户端在网络故障时重试

旁注:使用 PUT 创建资源是值得怀疑的。虽然在规范中这是可能的,但我不认为这是一个好主意,就像使用 POST 执行搜索不是一个好主意一样,就像通过 HTTP 建立 SOAP 隧道也不是一个好主意一样。 AtomPub 明确指出 PUT 不用于创建原子条目

POST 的普遍存在源于这样一个事实:HTML 定义了

元素,这些元素导致 POST 一个 application/x-www-form-urlencoded实体,接收者可以用它做任何它想做的事情,包括

  • 创建从属资源(响应通常伴随着 201 响应和 Location 标头)
  • 创建完全不同的资源(通常是 201 响应和 Location 标头)
  • 创建许多从属和/或不相关的资源(可能有一个简单的响应指示所创建资源的 URI),
  • 除了返回一个响应(例如 200302)(可能应该使用 GET 的情况)
  • 修改接收 POST 本身的资源(返回或重定向回更新的资源) 。
  • 删除一项或多项资源。
  • 上述任意组合。

唯一知道 POST 请求中会发生什么的人是发起请求的用户(通过单击巨大的“是的,我确认删除我的 Facebook 个人资料”按钮)和处理该请求的服务器。对于世界其他地方来说,该请求是不透明的,除了“此 URI 正在传递一些数据”之外没有任何意义。

因此,您问题的答案是 POSTPUT 都可以用于创建和更新。

  1. POST 通常用于创建资源(例如 AtomPub 9.2
  2. PUT 语义非常适合修改资源(例如 AtomPub 9.3)
  3. POST 可用于修改资源(如 www 表单编辑您的个人资料)
  4. 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 of GET. The usual flow is that you GET 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 of PUT 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 to PUT'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.

POSTs ubiquitousness comes from the fact that HTML defines <form> elements that result in POSTing a application/x-www-form-urlencoded entity, with which the recipient can do anything it pleases, including

  • creating subordinate resources (The repsonse is usually accompanied by a 201 response and Location header)
  • creating a completely different resource (again usually a 201 response and Location header)
  • creating many subordinate and/or unrelated resources (perhaps with a simple response indicating the URIs of the created resources)
  • doing nothing except return a response (e.g. 200 or 302) (a case where perhaps GET should have been used)
  • modifying the resource that received the POST itself (returning or redirecting back to the updated resource).
  • delete one or more resource.
  • any combination of the above.

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 and PUT can be used for both create and update.

  1. POST is often use to create resources (like AtomPub 9.2)
  2. PUT semantics fits well for modifying resources (like AtomPub 9.3)
  3. POST may be used to modify resources (like a www form edit your profile)
  4. PUT can technically be used to create resources (although I advise against it)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文