atkayla
atkayla

Reputation: 8849

REST API to create one or multiple resources?

The options I can think of are:

1

POST /book (object) - creates one book
POST /bulk/book (array) - creates multiple books

2

POST /book (object|array) - creates one or multiple books depending on the input type

3

POST /book (array) - expect an array every time, to create one book just have one in the array

Is there a recommendation?

Upvotes: 5

Views: 2558

Answers (2)

Ruckert Solutions
Ruckert Solutions

Reputation: 1301

I would go for 2 or 3 as well.

This is how I do it normally:

GET    /Books      => Get all books
GET    /Books/{id} => Get one book
POST   /Books      => Create new book
PUT    /Books/{id} => Updates all values of a book
PATCH  /Books/{id} => Updates/adds one or more value of a book
DELETE /Books/{id} => Deletes a given book

To make it easier for small projects, sometimes I merge Post, Put and Patch all into POST.

Upvotes: 1

cgdibble
cgdibble

Reputation: 72

My preference is towards 3, with 2 being runner up.

What I like is that regardless of how many books you want created, there is but one way to do so, and that is via POST /book (array).

This simplifies things for clients in that they don't have to use either a) a different endpoint b) a different data type depending on the number of books to be created. Need a book created? Perfect, there is one easy way to do so.

In the end, I see that as simplifying documentation, testing, and overall use.

Upvotes: 0

Related Questions