首页 > 其他 > 详细

从RFC2616看POST和PUT的区别

时间:2014-04-10 02:12:40      阅读:604      评论:0      收藏:0      [点我收藏+]

同事今天问我POST和PUT方法的区别,我也不能讲清楚,就去看了下RFC2616文档(计算机网络课的参考文献嘛,哈哈)

------------

下面是RFC2616对PUT和POST的区别的阐述:

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.

A single resource may be identified by many different URIs. For example, an article might have a URI for identifying "the current version" which is separate from the URI identifying each particular version. In this case, a PUT request on a general URI might result in several other URIs being defined by the origin server.


可以看出,POST方法和PUT方法的根本区别在于它们的Request-URI含义不同。

我的理解是:

POST方法传递的URI指定了操作资源的的一个程序;而PUT方法则是直接制定了要操作的资源

以上传一张图片为例:

POST(只是指向了mywebsite/upload 这个地址):

HTTP/1.1 PUT /mywebsite/upload/

PUT(直接制定了要操作的资源是201404091234.jpg):

HTTP/1.1 PUT /mywebsite/upload/201404091234.jpg

另一个重要的区别是,PUT方法是“幂等”的,而POST方法不是。

首先看下RFC2612是怎样定义“幂等(idempotence”的:

            Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is             the same as for a single request. The methods GET, HEAD, PUT and DELETE share this property. Also, the methods OPTIONS and TRACE should               not have side effects, and so are inherently idempotent.


也就是,所谓的幂等,就是对请求N次和请求1次的结果是一样的。GET、HEAD、PUT和DELETE都是幂等的,而POST不是。

还是以上传图片为例:

POST:

HTTP/1.1 PUT /mywebsite/upload/
可能会得到201404091234.jpg ,201404091235.jpg。。。

PUT:

HTTP/1.1 PUT /mywebsite/upload/201404091234.jpg
不论请求多少次都是201404091234.jpg (这就是“幂等的”)


参考:

1.http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.9.6

2.http://jcalcote.wordpress.com/2008/10/16/put-or-post-the-rest-of-the-story/

3.http://blog.csdn.net/mad1989/article/details/7918267

从RFC2616看POST和PUT的区别,布布扣,bubuko.com

从RFC2616看POST和PUT的区别

原文:http://blog.csdn.net/wusuopubupt/article/details/23260557

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!