在RESTful风格的编程中,我们应该使用HTTP方法作为构建块。我有点困惑,但是哪些方法与经典的CRUD方法相匹配。GET/Read和DELETE/ DELETE是显而易见的。
但是,PUT/POST之间有什么区别呢?它们是否与创建和更新一一匹配?
在RESTful风格的编程中,我们应该使用HTTP方法作为构建块。我有点困惑,但是哪些方法与经典的CRUD方法相匹配。GET/Read和DELETE/ DELETE是显而易见的。
但是,PUT/POST之间有什么区别呢?它们是否与创建和更新一一匹配?
当前回答
一般来说,这是我使用的模式:
HTTP获取-选择/请求 HTTP put - update 插入/创建 HTTP删除-删除
其他回答
一般来说,这是我使用的模式:
HTTP获取-选择/请求 HTTP put - update 插入/创建 HTTP删除-删除
目前(2016年)最新的HTTP动词是GET, POST, PATCH, PUT和DELETE
概述
HTTP获取-选择/请求 HTTP put - update 插入/创建 HTTP补丁——当放置一个完整的资源表示很麻烦并且占用了更多的带宽时,例如:当你必须部分更新一个列时 HTTP删除-删除
希望这能有所帮助!
如果你对设计REST api感兴趣,这是一本不错的读物!网站在线版本github资源库
我一直在寻找同样的答案,这是IBM说的。 IBM链接
POST创建一个新资源。 GET检索资源。 PUT更新现有资源。 DELETE删除资源。
youtube上有一个由stormpath制作的视频,它解释了这个问题,URL应该跳到视频的正确部分:
Stormpath YouTube视频
同样值得一看的是,这是一个多小时的演讲,但如果你想花时间构建一个REST api,这是非常有趣的。
REST的构建块主要是资源(和URI)和超媒体。在这个上下文中,GET是获取资源表示的方法(在CRUD术语中,它确实可以映射到SELECT)。
但是,您不应该期望CRUD操作和HTTP谓词之间有一对一的映射。 PUT和POST的主要区别在于它们的幂等性。POST也更常用于部分更新,因为PUT通常意味着发送资源的全新表示形式。
我建议你读一读:
http://roy.gbiv.com/untangled/2009/it-is-okay-to-use-post http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven
HTTP规范也是一个有用的参考:
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,