我应该在REST API中使用PATCH或PUT吗? [英] Should I use PATCH or PUT in my REST API?

查看:275
本文介绍了我应该在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吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆