我应该在REST API中使用PATCH或PUT吗?

编程入门 行业动态 更新时间:2024-10-26 02:32:32
本文介绍了我应该在REST API中使用PATCH或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.

我应该将终点设计为

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 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吗?

本文发布于:2023-11-27 05:35:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636809.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:API   REST   PUT   PATCH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!