哪些 HTTP 方法与哪些 CRUD 方法匹配?

发布于 2024-11-11 04:26:09 字数 154 浏览 4 评论 0 原文

在 RESTful 风格的编程中,我们应该使用 HTTP 方法作为我们的构建块。我有点困惑哪些方法与经典的 CRUD 方法相匹配。 GET/Read 和 DELETE/Delete 是显而易见的。

但是,PUT/POST 之间有什么区别?它们是否与“创建”和“更新”一对一匹配?

In RESTful style programming, we should use HTTP methods as our building blocks. I'm a little confused though which methods match up to the classic CRUD methods. GET/Read and DELETE/Delete are obvious enough.

However, what is the difference between PUT/POST? Do they match one to one with Create and Update?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

情深缘浅 2024-11-18 04:26:09
Create = PUT with a new URI
         POST to a base URI returning a newly created URI
Read   = GET
Update = PUT with an existing URI
Delete = DELETE

PUT 可以映射到 Create 和 Update,具体取决于 PUT 所使用的 URI 是否存在。

POST 映射到 Create。

更正:POST 也可以映射到 Update,尽管它通常用于 Create。 POST 也可以是部分更新,因此我们不需要建议的 PATCH 方法。

Create = PUT with a new URI
         POST to a base URI returning a newly created URI
Read   = GET
Update = PUT with an existing URI
Delete = DELETE

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.

混浊又暗下来 2024-11-18 04:26:09

关键在于您是否进行幂等更改。也就是说,如果对消息执行两次操作将导致“相同”的事情出现,就好像只执行了一次一样,那么您就获得了幂等更改,并且应该将其映射到 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.)

灵芸 2024-11-18 04:26:09

我正在寻找相同的答案,这是 IBM 的说法。
IBM 链接

POST 创建新资源。
GET 检索资源。
PUT 更新现有资源。
DELETE 删除资源。

I Was searching for the same answer, here is what IBM say.
IBM Link

POST            Creates a new resource.
GET             Retrieves a resource.
PUT             Updates an existing resource.
DELETE          Deletes a resource.
十级心震 2024-11-18 04:26:09

目前(2016 年)最新的 HTTP 动词是 GET、POST、PATCH、PUT 和

DELETE

  • HTTP GET - 选择/请求
  • HTTP PUT - 更新
  • HTTP POST - 插入/创建
  • HTTP PATCH - 何时PUT设置完整的资源表示很麻烦并且会占用更多带宽,例如:当您必须部分更新列时
  • HTTP DELETE - DELETE

希望这会有所帮助!

如果您对设计 REST API 感兴趣,那么这本书值得一读! 网站在线版 github 存储库

Right now (2016) the latest HTTP verbs are GET, POST, PATCH, PUT and DELETE

Overview

  • HTTP GET - SELECT/Request
  • HTTP PUT - UPDATE
  • HTTP POST - INSERT/Create
  • HTTP PATCH - When PUTting a complete resource representation is cumbersome and utilizes more bandwidth, e.g.: when you have to update partially a column
  • HTTP DELETE - DELETE

Hope this helps!

If you are interested on designing REST APIs this is an ansewome reading to have! website online version github repository

于我来说 2024-11-18 04:26:09

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.

攀登最高峰 2024-11-18 04:26:09

这取决于具体情况..但一般来说:

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

仙女 2024-11-18 04:26:09

REST 的构建块主要是资源(和 URI)和超媒体。在这种情况下,GET 是获取资源表示的方式(实际上可以在 CRUD 术语中映射到 SELECT)。

但是,您不一定期望 CRUD 操作和 HTTP 动词之间存在一对一的映射。
PUTPOST 之间的主要区别在于它们的幂等属性。 POST 也更常用于部分更新,因为 PUT 通常意味着发送资源的全新表示形式。

我建议阅读以下内容:

HTTP 规范也是一个有用的参考:

PUT 方法要求
封闭的实体存储在
提供请求 URI。

[...]

两者之间的根本区别
POST 和 PUT 请求体现在
的不同含义
请求 URI。 POST 请求中的 URI
标识将要使用的资源
处理封闭的实体。那
资源可能是数据接受
流程,通向其他流程的门户
协议,或一个单独的实体
接受注释。相比之下,
PUT 请求中的 URI 标识
请求中包含的实体——
用户代理知道 URI 是什么
预期的,服务器不得
尝试将请求应用于某些
其他资源。如果服务器需要
该请求适用于
不同的 URI,

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 a SELECT 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 and POST is about their idempotent property. POST is also more commonly used for partial updates, as PUT generally implies sending a full new representation of the resource.

I'd suggest reading this:

The HTTP specification is also a useful reference:

The PUT method requests that the
enclosed entity be stored under the
supplied Request-URI.

[...]

The fundamental difference between the
POST and PUT requests is reflected in
the different meaning of the
Request-URI. The URI in a POST request
identifies the resource that will
handle the enclosed entity. That
resource might be a data-accepting
process, a gateway to some other
protocol, or a separate entity that
accepts annotations. In contrast, the
URI in a PUT request identifies the
entity enclosed with the request --
the user agent knows what URI is
intended and the server MUST NOT
attempt to apply the request to some
other resource. If the server desires
that the request be applied to a
different URI,

老娘不死你永远是小三 2024-11-18 04:26:09

一般来说,这是我使用的模式:

  • HTTP GET - SELECT/Request
  • HTTP PUT - UPDATE
  • HTTP POST - INSERT/Create
  • HTTP DELETE - DELETE

Generally speaking, this is the pattern I use:

  • HTTP GET - SELECT/Request
  • HTTP PUT - UPDATE
  • HTTP POST - INSERT/Create
  • HTTP DELETE - DELETE
幸福丶如此 2024-11-18 04:26:09

Symfony 项目尝试将其 HTTP 方法与 CRUD 方法结合起来,并且 它们的列表将它们关联起来,如下所示:

  • GET 从服务器检索资源
  • POST 在服务器上创建资源
  • PUT 更新资源在服务器上
  • DELETE 从服务器上删除资源

值得注意的是,正如他们在该页面上所说的那样,“实际上,许多现代浏览器不支持 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:

  • GET Retrieve the resource from the server
  • POST Create a resource on the server
  • PUT Update the resource on the server
  • DELETE Delete the resource from the server

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文