对着rfc2616来看restful api中post和put的区别

当需要使我们的api更restful化的时候,我们得选择http的动词作为资源的crud动作,其中 Read -> Get , Delete -> Delele 这两个没什么争议, 而 Create 和 Update 对应 Post 还是 Put ?

先给出正确的对应关系: Create -> Post , Update -> Put , 再接着对着 rfc2616 来解释为什么

  1. POST

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions:

POST 方法用来请求源服务器接收附于请求中的实体,把它为作为请求行中 URI 所标识的资源的从属。 POST 被设计来作为统一的方法以覆盖以下几种功能:

Annotation of existing resources;

对已有资源的注解

Posting a message to a bulletin board, newsgroup, mailing list,
or similar group of articles;

提交消息到布告板,新闻组,邮件列表或类似的论文组中

Providing a block of data, such as the result of submitting a form, to a data-handling process;

提交一段数据,比如表单提交中的数据,到数据处理程序

Extending a database through an append operation.

通过新增操作库充数据库(个人理解为 增加记录)

The actual function performed by the POST method is determined by the server and is usually dependent on the Request-URI. The posted entity is subordinate to that URI in the same way that a file is subordinate to a directory containing it, a news article is subordinate to a newsgroup to which it is posted, or a record is subordinate to a database.

POST 方法的实际效果由服务器决定,而且通常也依赖于请求的URI。 被提交的实体从属于 URI 的方式就像文件从属于包含它的目录(文件夹)一样。比如,一条新闻从属于它发布的新闻组,一条记录属于一个数据库。

The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.

POST 方法执行的结果可能并不能由 URI 所标识。 这样的话,200(OK) 或 204 (No Content)都是合适的响应码,这依赖于相应是否包含描述结果的实体。

大白话 Post

先看看 post 的英文注释:



有道词典中,post作为及物动词时候的意思是 张贴、公布,比如在布告栏上张贴一个告示,目标是公告栏,公告栏可以贴好多个告示,一个告示是公告栏的一部分。

再看rfc2616中对post语义的描述,我们就有个印象 post 其实是“部分提交”(注意原文中的 subordinate 这个词),目标uri应该是一个资源组,如 向
订单列表 supermall.holysu.com/customer/{userid}/orders 提交一个订单, post的数据为 orders 中的一个order ; 或者上面几种情况中的第一条,post 提交的是uri所标识资源的注释;然后再根据语义想想我们平时更新数据的时候向 orders/{id} 提交数据就有点奇怪了吧 总而言之,post 向目标uri提交的是这个uri资源的附属资源,可以是注释、一个成员、一条记录或者其一部分。

然而大多数人平时开发并没有遵守http的谓词语义,就比如我个人之前做接口开发的时候,所有请求不管是查询、新增、修改还是删除都是通过post来发送参数的

// request
{
      param1:xx,
      param2:xx,
}
// response
{
    code: 1,
    msg: "success",
    data:{}
}

之前只是考虑是方式统一,但是这一点都不restful。对前端接口本身是前后端的一个协议,而我们通过http进行交互的话,如果遵守http协议定义的语义的话,就能享受到更多的便利,如http条件缓存、减少歧义、降低沟通难度、方便接入 etc。不同的标准越少不就更利于沟通协作么,不然那个什么 DDD (最近突然很火)中倡导的领域统一语言就不会那么重要了。

  1. PUT

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI. If a new resource is created, the origin server MUST inform the user agent via the 201 (Created) response. If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request. If the resource could not be created or modified with the Request-URI, an appropriate error response SHOULD be given that reflects the nature of the problem. The recipient of the entity MUST NOT ignore any Content-*(e.g. Content-Range) headers that it does not understand or implement and MUST return a 501 (Not Implemented) response in such cases.

put 方法请求将实体存储在请求的uri中。如果请求uri 指向已存在的资源,则应该考虑将请求实体作为服务端那份已有资源的修改版本;如果请求 uri 并没有指向一个已有资源,并且这个 uri 可以被发出请求的用户代理定义为一个新的资源,那么源服务器就能用这个 uri 创建新的资源。 如果一个新资源创建好了,源服务器必须以 201(Created)的响应码来告知用户代理(浏览器)。如果已存在资源被修改了,发送响应码 200(OK)或 204(No Content)来表示请求已完成。如果请求 uri 标识的资源不能创建或不能修改,需要给出合适的错误相应来反应问题的原因。请求实体的接受方不能忽视任何 Content-*(比如 Content-Range)这样他不识别或没实现的请求头,这种情况必须返回 501(Not Implemented)。

If the request passes through a cache and the Request-URI identifies one or more currently cached entities, those entries SHOULD be treated as stale. Responses to this method are not cacheable.

如果请求通过缓存,并且请求uri识别到1个多个缓存实体,那些实体应该视为过时的。对put方法的响应不能缓存。

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, it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request.

POST 和 PUT 请求的基本区别反应在请求uri的含义上。在 POST 请求中 URI 标识的是处理请求实体的资源。这个资源可能是数据处理程序,针对其他协议的网关,或者一个接收注解的单独的实体。相反,PUT 请求中的 URI 标识的就是请求实体,用户代理(浏览器)知道 URI 所指向的是什么,而且服务端不能试图将请求应用到其他资源上。如果服务端想要将请求应用到一个不同的 URI 上,它必须发送 301(Moved Permanently)响应;接着浏览器可以自己决定是否重定向这个请求。

大白话 put


put,及物动词的含义是 “放”,那么这个动作的目标,放在哪里,是确定的;

举个例子,新建和更新客户信息

场景一,新建:用put来做的话就是往 xxx/customer/{id} 这uri上放一个新的客户,而这个 id 应该是服务端不存在的、新的; 成功应该返回 201(Created)

场景二,更新: 更新客户id为1的客户信息,向 xxx/customer/1 put 数据

{
    "name": "holysu",
    "post": "awesome coder"
}

服务端存在这个uri标识的资源并且修改成功的话返回 200(ok) ,如果不存在这个客户则返回 204(No Content)

而由于资源的唯一标识是由服务端生成的,比如订单id一般是服务端通过一定的业务规则生成的,所以在创建订单我们会选择以向 supermall.holysu.com/customer/{userid}/orders 这个uri post一个订单数据的方式来创建, 这就是为啥文章开始说 Create 对应的是 Post了。

个人的理解路径是: 英文单词的含义思考这两个动词的含义 -> rfc的定义(uri的含义) -> 实际使用场景 crud 的对应关系

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容