使用restful HTTP创建单个和多个资源 [英] Create single and multiple resources using restful HTTP
问题描述
在我的API服务器中,我定义了这条路线:
In my API server I have this route defined:
POST /categories
要创建一个类别:
POST /categories {"name": "Books"}
我想如果你想创建多个类别,那么你可以这样做:
I thought that if you want to create multiple categories, then you could do:
POST /categories [{"name": "Books"}, {"name": "Games"}]
我只想确认这是Restful HTTP API的一个好习惯。
I just wanna confirm that this is a good practice for Restful HTTP API.
或者应该有一个
POST /bulk
允许他们一次做任何操作(创建,阅读,更新和删除)?
for allowing them to do whatever operations at once (Creating, Reading, Updating and Deleting)?
推荐答案
在真正的REST中,您应该在多个单独的调用中对此进行POST。原因是每一个都会产生新的表示。如果不这样的话,你会怎么想呢?
In true REST, you should probably POST this in multiple separate calls. The reason is that each one will result in a new representation. How would you expect to get that back otherwise.
每个帖子都应返回结果资源位置:
Each post should return the resultant resource location:
POST -> New Resource Location
POST -> New Resource Location
...
但是,如果您需要批量,请创建一个块。尽可能教条,但如果没有,实用主义就能完成工作。如果你太依赖于教条主义,那么你永远不会做任何事情。
However, if you need a bulk, then create a bulk. Be dogmatic where possible, but if not, pragmatism gets the job done. If you get too hung up on dogmatism, then you never get anything done.
这篇关于使用restful HTTP创建单个和多个资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!