我应该在我的 REST API 中使用 PATCH 还是 PUT? [英] Should I use PATCH or PUT in my REST API?
问题描述
我想为以下场景使用适当的方法设计我的休息端点.
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.
我应该将终点设计为
PUT /groups/api/v1/groups/{group id}/status/activate
或
PATCH /groups/api/v1/groups/{group id}
with request body like
{action:activate|deactivate}
推荐答案
PATCH
方法在这里是正确的选择,因为您正在更新现有资源 - 组 ID.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 its 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屋!