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

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


当前回答

这要看具体情况而定。但总的来说:

PUT =用资源的具体URI更新或更改具体资源。

POST =在给定URI的源下创建一个新资源。

I.e.

编辑一篇博客:

输入: /博客/录入/ 1

创建一个新的:

发布: /博客/条目

PUT可以在某些情况下创建一个新资源,其中新资源的URI在请求之前是明确的。 POST也可以用来实现其他用例(GET、PUT、DELETE、HEAD、OPTIONS)所不涵盖的其他用例。

对于CRUD系统的一般理解是GET =请求,POST =创建,Put =更新,DELETE =删除

其他回答

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

HTTP获取-选择/请求 HTTP put - update 插入/创建 HTTP删除-删除

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.)

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,尽管它通常用于创建。POST也可以是部分更新,所以我们不需要建议的PATCH方法。

这要看具体情况而定。但总的来说:

PUT =用资源的具体URI更新或更改具体资源。

POST =在给定URI的源下创建一个新资源。

I.e.

编辑一篇博客:

输入: /博客/录入/ 1

创建一个新的:

发布: /博客/条目

PUT可以在某些情况下创建一个新资源,其中新资源的URI在请求之前是明确的。 POST也可以用来实现其他用例(GET、PUT、DELETE、HEAD、OPTIONS)所不涵盖的其他用例。

对于CRUD系统的一般理解是GET =请求,POST =创建,Put =更新,DELETE =删除

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

概述

HTTP获取-选择/请求 HTTP put - update 插入/创建 HTTP补丁——当放置一个完整的资源表示很麻烦并且占用了更多的带宽时,例如:当你必须部分更新一个列时 HTTP删除-删除

希望这能有所帮助!

如果你对设计REST api感兴趣,这是一本不错的读物!网站在线版本github资源库