用于添加到 RESTful API 中的集合的 HTTP 方法 [英] HTTP Method to use for adding to a collection in a RESTful API
问题描述
我有一个代表标签集合的资源:
I have a resource that represents a collection of tags:
/users/{username}/tags
API 客户端应该能够在单个 HTTP 请求中向该集合添加一组标签.我考虑过如何做到这一点,首先考虑使用 PUT 或 POST 方法.但是我认为这意味着客户端正在设置"或替换"该集合中的标签.将多个标签添加"到该集合中的最合适的 HTTP 方法(或者可能是不同的机制)是什么?
An API client should be able to add a set of tags to this collection in a single HTTP request. I thought about how to do this and first thought about using the PUT or POST methods. However I think this would imply that the client is "setting" or "replacing" the tags in that collection. What would be the most appropriate HTTP method (or perhaps a different mechanism) to "add" multiple tags to that collection?
{HTTP METHOD} /users/{username}/tags
请求正文:
["short", "crazy", "funny"]
推荐答案
如果在请求正文中发送的标签旨在添加到集合中,而不是替换,我建议使用 POST.如果您打算替换现有标签,请使用 PUT.
If the tags being sent in the request body are intended to be added to a collection, rather than replace, I would suggest POST. If you intend to replace the existing tags, use PUT.
这篇关于用于添加到 RESTful API 中的集合的 HTTP 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!