哪些 HTTP 方法与哪些 CRUD 方法匹配?
在 RESTful 风格的编程中,我们应该使用 HTTP 方法作为我们的构建块。我有点困惑哪些方法与经典的 CRUD 方法相匹配。 GET/Read 和 DELETE/Delete 是显而易见的。
但是,PUT/POST 之间有什么区别?它们是否与“创建”和“更新”一对一匹配?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
PUT 可以映射到 Create 和 Update,具体取决于 PUT 所使用的 URI 是否存在。
POST 映射到 Create。
更正:POST 也可以映射到 Update,尽管它通常用于 Create。 POST 也可以是部分更新,因此我们不需要建议的 PATCH 方法。
PUT can map to both Create and Update depending on the existence of the URI used with the PUT.
POST maps to Create.
Correction: POST can also map to Update although it's typically used for Create. POST can also be a partial update so we don't need the proposed PATCH method.
关键在于您是否进行幂等更改。也就是说,如果对消息执行两次操作将导致“相同”的事情出现,就好像只执行了一次一样,那么您就获得了幂等更改,并且应该将其映射到 PUT。如果没有,则映射到 POST。如果您从不允许客户端合成 URL,PUT 与 Update 非常接近,而 POST 可以很好地处理 Create,但这肯定不是唯一的方法;如果客户端知道它想要创建
/foo/abc
并且知道要放在那里的内容,那么它就像 PUT 一样工作得很好。POST 的规范描述是当您承诺购买某些东西时:这是一个没有人愿意在不知情的情况下重复的操作。相比之下,预先设置订单的派送地址可以使用 PUT 来完成:无论是否被告知发送到
6 Anywhere Dr, Nowhereville
一次、两次或一百次,都没有关系: 还是同一个地址。这是否意味着这是一个更新?可能......这完全取决于你想如何编写后端。 (请注意,结果可能不相同:您可以向用户报告他们上次执行 PUT 的时间,作为资源表示的一部分,这将确保重复的 PUT 不会导致相同的结果,但结果仍然会在功能意义上是“相同”。)The whole key is whether you're doing an idempotent change or not. That is, if taking action on the message twice will result in “the same” thing being there as if it was only done once, you've got an idempotent change and it should be mapped to PUT. If not, it maps to POST. If you never permit the client to synthesize URLs, PUT is pretty close to Update and POST can handle Create just fine, but that's most certainly not the only way to do it; if the client knows that it wants to create
/foo/abc
and knows what content to put there, it works just fine as a PUT.The canonical description of a POST is when you're committing to purchasing something: that's an action which nobody wants to repeat without knowing it. By contrast, setting the dispatch address for the order beforehand can be done with PUT just fine: it doesn't matter if you are told to send to
6 Anywhere Dr, Nowhereville
once, twice or a hundred times: it's still the same address. Does that mean that it's an update? Could be… It all depends on how you want to write the back-end. (Note that the results might not be identical: you could report back to the user when they last did a PUT as part of the representation of the resource, which would ensure that repeated PUTs do not cause an identical result, but the result would still be “the same” in a functional sense.)我正在寻找相同的答案,这是 IBM 的说法。
IBM 链接
I Was searching for the same answer, here is what IBM say.
IBM Link
目前(2016 年)最新的 HTTP 动词是 GET、POST、PATCH、PUT 和
DELETE
希望这会有所帮助!
如果您对设计 REST API 感兴趣,那么这本书值得一读! 网站在线版 github 存储库
Right now (2016) the latest HTTP verbs are GET, POST, PATCH, PUT and DELETE
Overview
Hope this helps!
If you are interested on designing REST APIs this is an ansewome reading to have! website online version github repository
Stormpath 有一个很棒的 YouTube 视频讲座,实际上解释了这一点,URL 应跳至视频的正确部分:
stormpath youtube 视频
另外,如果您考虑投入时间进行构建,也值得观看一个多小时的谈话,但非常有趣一个 REST API。
There's a great youtube video talk by stormpath with actually explains this, the URL should skip to the correct part of the video:
stormpath youtube video
Also it's worth watch it's over an hour of talking but very intersting if your thinking of investing time in building a REST api.
这取决于具体情况..但一般来说:
PUT = 使用资源的具体 URI 更新或更改具体资源。
POST = 在给定 URI 的源下创建一个新资源。
即
编辑博客文章:
PUT:
/blog/entry/1
创建一个新博客:
POST:
在某些情况下, /blog/entry
PUT 可能会创建新资源,其中新资源的 URI 在请求之前就已明确。
POST 也可用于实现其他几种未涵盖的用例(GET、PUT、DELETE、HEAD、OPTIONS)。
CRUD 系统的一般理解是 GET = 请求、POST = 创建、Put = 更新,删除=删除
It depends on the concrete situation.. but in general:
PUT = update or change a concrete resource with a concrete URI of the resource.
POST = create a new resource under the source of the given URI.
I.e.
Edit a blog post:
PUT:
/blog/entry/1
Create a new one:
POST:
/blog/entry
PUT may create a new resource in some circumstances where the URI of the new ressource is clear before the request.
POST can be used to implement several other use cases, too, which are not covered by the others (GET, PUT, DELETE, HEAD, OPTIONS)
The general understanding for CRUD systems is GET = request, POST = create, Put = update, DELETE = delete
REST 的构建块主要是资源(和 URI)和超媒体。在这种情况下,
GET
是获取资源表示的方式(实际上可以在 CRUD 术语中映射到SELECT
)。但是,您不一定期望 CRUD 操作和 HTTP 动词之间存在一对一的映射。
PUT
和POST
之间的主要区别在于它们的幂等属性。POST
也更常用于部分更新,因为PUT
通常意味着发送资源的全新表示形式。我建议阅读以下内容:
HTTP 规范也是一个有用的参考:
The building blocks of REST are mainly the resources (and URI) and the hypermedia. In this context,
GET
is the way to get a representation of the resource (which can indeed be mapped to aSELECT
in CRUD terms).However, you shouldn't necessarily expect a one-to-one mapping between CRUD operations and HTTP verbs.
The main difference between
PUT
andPOST
is about their idempotent property.POST
is also more commonly used for partial updates, asPUT
generally implies sending a full new representation of the resource.I'd suggest reading this:
The HTTP specification is also a useful reference:
一般来说,这是我使用的模式:
Generally speaking, this is the pattern I use:
Symfony 项目尝试将其 HTTP 方法与 CRUD 方法结合起来,并且 它们的列表将它们关联起来,如下所示:
值得注意的是,正如他们在该页面上所说的那样,“实际上,许多现代浏览器不支持 PUT 和 DELETE 方法。”
据我所知,Symfony 在生成表单时为那些不支持 PUT 和 DELETE 的浏览器“伪造”PUT 和 DELETE,以便尝试尽可能接近使用理论上正确的 HTTP 方法,即使浏览器不支持它。
The Symfony project tries to keep its HTTP methods joined up with CRUD methods, and their list associates them as follows:
It's worth noting that, as they say on that page, "In reality, many modern browsers don't support the PUT and DELETE methods."
From what I remember, Symfony "fakes" PUT and DELETE for those browsers that don't support them when generating its forms, in order to try to be as close to using the theoretically-correct HTTP method even when a browser doesn't support it.