简体   繁体   中英

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

I have a resource class . I need two APIs :

  1. bulk create classes
  2. create one class.

In the REST standards should I create 2 APIs:

  1. /classes/bulk/ --- for bulk create
  2. /classes/ --- to create one class.

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 ?

Also, the processing logic and the response schema for 200 is different for the bulk and the non bulk call

This approach doesn't seem to RESTful to me or is it?

The URI spelling is not relevant in the REST architectural. Appending /bulk to the URI or not won't make your application more RESTful or not.


Alternatively to use /bulk in the URI, you could use the same URI for handling the creation of one or multiple resources. 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. If the operation is not atomic you could use 207 to report the result of the operation of each resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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