简体   繁体   English

REST API:在API上附加“批量”以在同一资源上创建批量创建?

[英]REST API: append 'bulk' to api to create bulk create on same resource?

I have a resource class . 我有一个资源class I need two APIs : 我需要两个API:

  1. bulk create classes 批量创建类
  2. create one class. 创建一个类。

In the REST standards should I create 2 APIs: 在REST标准中,我应该创建2个API:

  1. /classes/bulk/ --- for bulk create /classes/bulk/ -用于批量创建
  2. /classes/ --- to create one class. /classes/ -创建一个类。

This approach doesn't seem to REStFul to me or is it? 这种方法对我来说似乎不是必需的,是吗? Is it more RESTful to use query param for this : /classes?type=bulk ? 为此使用查询参数是否更RESTful: /classes?type=bulk

Also, the processing logic and the response schema for 200 is different for the bulk and the non bulk call 同样,批量调用和非批量调用的200的处理逻辑和响应模式也不同

This approach doesn't seem to RESTful to me or is it? 这种方法对我来说似乎不是RESTful的?

The URI spelling is not relevant in the REST architectural. URI拼写与REST体系结构无关。 Appending /bulk to the URI or not won't make your application more RESTful or not. 是否将/bulk附加到URI不会使您的应用程序更加RESTful。


Alternatively to use /bulk in the URI, you could use the same URI for handling the creation of one or multiple resources. 要在URI中使用/bulk ,也可以使用相同的URI处理一个或多个资源的创建。 Consider that your resource is a message . 考虑到您的资源是一条消息 Then you could have the following: 然后,您可能会得到以下内容:

POST /api/messages HTTP/1.1
Host: example.org
Content-Type: application/json

{ 
  "to": "John"
  "content": "Hi"
}
POST /api/messages HTTP/1.1
Host: example.org
Content-Type: application/json

[
  { 
    "to": "John"
    "content": "Hi"
  },
  { 
    "to": "James"
    "content": "Hey"
  },
  { 
    "to": "Sarah"
    "content": null
  }
]

It's also important to keep in mind whether the operation will be atomic or not. 记住操作是否为原子操作也很重要。 If one message contains invalid data, you may or may not reject the whole request. 如果一封邮件中包含无效数据,则您可以拒绝也可以不拒绝整个请求。

If the operation is atomic , you can use 400 or 422 to report invalid data. 如果操作是原子操作, 可以使用400422报告无效数据。 If the operation is not atomic you could use 207 to report the result of the operation of each resource. 如果该操作不是原子操作, 可以使用207报告每个资源的操作结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM