在RESTful风格的编程中,我们应该使用HTTP方法作为构建块。我有点困惑,但是哪些方法与经典的CRUD方法相匹配。GET/Read和DELETE/ DELETE是显而易见的。
但是,PUT/POST之间有什么区别呢?它们是否与创建和更新一一匹配?
在RESTful风格的编程中,我们应该使用HTTP方法作为构建块。我有点困惑,但是哪些方法与经典的CRUD方法相匹配。GET/Read和DELETE/ DELETE是显而易见的。
但是,PUT/POST之间有什么区别呢?它们是否与创建和更新一一匹配?
当前回答
我一直在寻找同样的答案,这是IBM说的。 IBM链接
POST创建一个新资源。 GET检索资源。 PUT更新现有资源。 DELETE删除资源。
其他回答
youtube上有一个由stormpath制作的视频,它解释了这个问题,URL应该跳到视频的正确部分:
Stormpath YouTube视频
同样值得一看的是,这是一个多小时的演讲,但如果你想花时间构建一个REST api,这是非常有趣的。
我一直在寻找同样的答案,这是IBM说的。 IBM链接
POST创建一个新资源。 GET检索资源。 PUT更新现有资源。 DELETE删除资源。
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.)
这要看具体情况而定。但总的来说:
PUT =用资源的具体URI更新或更改具体资源。
POST =在给定URI的源下创建一个新资源。
I.e.
编辑一篇博客:
输入: /博客/录入/ 1
创建一个新的:
发布: /博客/条目
PUT可以在某些情况下创建一个新资源,其中新资源的URI在请求之前是明确的。 POST也可以用来实现其他用例(GET、PUT、DELETE、HEAD、OPTIONS)所不涵盖的其他用例。
对于CRUD系统的一般理解是GET =请求,POST =创建,Put =更新,DELETE =删除
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方法。