RESTful API设计中如何实现批量操作


RESTful API设计中如何实现批量操作?比如一次请求删除N个ID,类似ElasticSearch中bulk那种设计。

python ruby java php go

天使的颤栗 9 years, 6 months ago
比利丶海灵顿 answered 9 years, 6 months ago

Laplace answered 9 years, 6 months ago

批量操作主要是js的操作,获取多个id后用ajax把id集合发送给一个action,具体操作写在action中。
举个例子,routes可以这样写


 resources :shops do
  collection do
    post :association_shops
  end
end

vvither answered 9 years, 6 months ago

设计方法有很多种,借用Backbone.js 的说法: There's More Than One Way To Do It ,不要太纠结。

简单的说,下面几种都不错:

  • GET /ec2/instance/batch?id=aa,bb,cc :简约的设计
  • GET /ec2/instance?batch={"ids":["aa","bb","cc"]} :健壮的设计
  • GET /ec2/instance?batch=[{"method":"DELETE","id":"aa"},{"method":"DELETE","id":"bb"},{"method":"DELETE","id":"cc"}] facebook 的设计 (墙外)

把上面的 GET 都改成 POST 也没什么问题。

设计 batch 的时候就不用太纠结于 RESTful 的定义(至少我是这样觉得),毕竟是为了优化性能而存在的东西。

actfate answered 9 years, 6 months ago

Your Answer