HttP之PATCH请求,这个平时用的不是很多,所以这里记录一下
1.PUT 和 PATCH
根据约定( Convention ),PUT 方法用于更新数据,PATCH 方法也用于更新数据,为什么 PUT 方法是幂等的而 PATCH 方法不是幂等的呢?我们继续研究文档(第54页)
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.
PUT 方法将请求所包含的实体存储在所提供的 Request-URI 下。如果该 URI 指代一个已经存在的资源,那么请求中的实体应该被视为保存在原服务器上的实体的修改版本。如果 Request-URI 没有指向一个现有资源,
并且该 URI 可以被发送请求的用户代理定义为新资源,则原服务器可以使用该 URI 来创建资源。
The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request-URI. The set of changes is represented
in a format called a “patch document” identified by a media type. If the Request-URI does not point to an existing resource, the server MAY create a new resource, depending
on the patch document type (whether it can logically modify a null resource) and permissions, etc.
PATCH 方法请求将一组描述在请求实体里的更改应用到 Request-URI 标志的资源。这组更改以称为 “补丁文档” 的格式(该格式由媒体类型标志)表示,如果 Request-URI 未指向现有资源,服务器可能根据
补丁文档的类型(是否可以在逻辑上修改空资源)和权限等来创建一个新资源。
幂等性:用户对于同一操作发起的一次请求或者多次请求的结果是一致的,不会因为多次点击而产生了副作用。
除了 POST和 PATCH ,其它的请求方法(GET PUT DELETE TRACE OPTIONS 等) 都是幂等的.
各个方法的详细说明:
GET
特点:安全、幂等。
说明:从服务器端获取数据,请求body在地址栏上。
作用:获取资源。
HEAD
特点:安全、幂等。
说明:与get方法类似,但不返回message body内容,仅仅是获得获取资源的部分信息(content-type、content-length)。
作用:restful框架中较少使用。
POST
特点:非安全、非幂等。
说明:向服务器端提交数据,请求数据在报文body里;
发送一个修改数据的请求,需求数据要重新新创建。
作用:用于创建子资源。创建、更新、删除、查询资源均可使用。
PUT
特点:非安全、幂等。
说明:向服务器端提交数据,请求数据在报文body里;
发送一个修改数据的请求,需求数据更新(全部更新)。
作用:用于创建、更新资源。
DELETE
特点:非安全、幂等。
说明:向服务器端提交数据,请求数据在报文body里;
发送一个删除数据的请求。
作用:删除资源。
OPTIONS
特点:安全、幂等。
作用:用于url验证,验证接口服务是否正常。
TRACE
特点:安全、幂等。
说明:维基百科“回显服务器收到的请求,这样客户端可以看到(如果有)哪一些改变或者添加已经被中间服务器实现。”
作用:restful框架中较少使用。
PATCH
特点:非安全、幂等。
说明:向服务器端提交数据,请求数据在报文body里;
与PUT类似,发送一个修改数据的请求,区别在于PATCH代表部分更新;
后来提出的接口方法,使用时可能去要验证客户端和服务端是否支持;
作用:用于创建、更新资源。局部更新,比如:user对象,只更改了name属性,那么他的其他属性值是不会变的,如果用post,那么其他属性值会被设置为null(全局更新)
