I'm trying to build an web API that closely represents a library of books. I currently have the following routes.
- GET /api/books
- GET /api/books/:bookID
- GET /api/libraries
- POST /api/libraries
- This route currently accepts a
books
parameter in the body (with the body represented as an object), that should be an array of bookIDs.
- This route currently accepts a
- GET /api/libraries/:libraryID
- DELETE /api/libraries/:libraryID
What I'm trying to decide is how to allow users to add/delete books to an existing library.
Option 1:
- POST /api/libraries/:libraryID/books/:bookID
- No body will be accepted in this route
- DELETE /api/libraries/:libraryID/books/:bookID
Option 2:
- POST /api/libraries/:libraryID/books
- Body will be an array of bookIDs represented as an array.
- DELETE /api/libraries/:libraryID/books
- Body will be an array of bookIDs represented as an array.
Option 3:
Some combination of 2 & 3, OR something completely different that I haven't thought of yet.
What is the most common method to handle this for a REST API? In the future I might also consider PUT /api/libraries/:libraryID
, but that would change the entire books
array, and replace it, as opposed to just adding or deleting a specific book(s).
/api
prefix from all of these URLs — it adds nothing and reinforces misconceptions that APIs are something different to websites.