用于在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屋!