REST API PATCH或PUT [英] REST API PATCH or PUT
问题描述
我想用以下场景的适当方法设计我的休息终点。
I want to design my rest endpoint with the appropriate method for the following scenario.
有一个组。每个组都有一个状态。管理员可以激活或停用该组。
There is a group. Each group has a status. The group can be activated or inactivated by the admin.
我应该将我的终点设计为
Should i design my end point as
PUT /groups/api/v1/groups/{group id}/status/activate
OR
PATCH /groups/api/v1/groups/{group id}
with request body like
{action:activate|deactivate}
推荐答案
在您更新现有资源(组ID)时, PATCH
方法是正确的选择。 PUT
只应在完全替换资源时使用。
The PATCH
method is the correct choice here as you're updating an existing resource - the group ID. PUT
should only be used if you're replacing a resource in it's entirety.
有关部分资源修改的更多信息,请参见 RFC 5789 。具体来说, PUT
方法描述如下:
Further information on partial resource modification is available in RFC 5789. Specifically, the PUT
method is described as follows:
扩展超文本的几个应用程序传输协议
(HTTP)需要一项功能来进行部分资源修改。
现有的HTTP PUT方法只允许完全替换
文档。此提议添加了一个新的HTTP方法PATCH,用于修改
现有HTTP资源。
Several applications extending the Hypertext Transfer Protocol (HTTP) require a feature to do partial resource modification. The existing HTTP PUT method only allows a complete replacement of a document. This proposal adds a new HTTP method, PATCH, to modify an existing HTTP resource.
这篇关于REST API PATCH或PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!