在很多 RESTfull 应用中,会使用到POST 方法去创建内容, PUT 方法去更新内容,例如

1
curl -x PUT /user/1 --data username=zonghua

但是经常混淆的是 PUT 和 PATCH 方法,从字面意义上理解就是,PUT 是重新放置, PATCH 进行局部修改。

当使用 id 做唯一主键的时候,就是 PUT 方法将一个指定 id 对象进行完全替换, PATCH 方法指定 id 去更新这个对象。

RFC 标准 https://tools.ietf.org/html/rfc5789#page-2

The difference between the PUT and PATCH requests is reflected in the
way the server processes the enclosed entity to modify the resource
identified by the Request-URI. In a PUT request, the enclosed entity
is considered to be a modified version of the resource stored on the
origin server, and the client is requesting that the stored version
be replaced. With PATCH, however, the enclosed entity contains a set
of instructions describing how a resource currently residing on the
origin server should be modified to produce a new version. The PATCH
method affects the resource identified by the Request-URI, and it
also MAY have side effects on other resources; i.e., new resources
may be created, or existing ones modified, by the application of a
PATCH.

发现微信小程序不给用 PATCH 方法
https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html#wxrequestobject

默认为 GET,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT