Some years ago, every API I developed was a REST API and I did everything to follow the best practices to make them "RESTful".
But after some time, I have my doubts if RESTful APIs that should represent a Domain are not even an anti pattern.
The most important reasons:
- The HTTP Methods like POST, PUT, DELETE and GET are often not expressive enough. For example a DELETE might be just a soft delete (setting a deleted flag to true), where its not clear which Method is the correct one (DELETE or PUT).
Also those methods are promoting a CRUD like API design but usually the use cases in a domain are not matching CRUD.
CRUD is not aligned with DDD. - The resource path design that RESTful APIs require is often not able to reflect the domain. For example a command (DDD) can affect many entities and its not possible to identify one "main" resource that should be addressed.
Adding a user to a group for example affects two resources. So a RESTful route could be like:
PUT: api:/users/{userId}/memberships/{groupId}
or
PUT: api:/groups/{groupId}/members/{userId}
It seems not clear and the whole resource style becomes more and more a burden.
Not to think of more complex scenarios where three or four entities are affected.
For me a command style would be a lot more expressive like:
POST: api:/addUserToGroup
Body:
{
UserId: 43,
GroupId: 12
}
- The HTTP status codes are not well defined. "Not found" for example should be used in HTTP if a route is not found. Not if a resource is not found.
My current idea is to just use two HTTP Methods: POST for commands and GET for queries and then build a CQRS API like design that represents my domain model a lot better.
All other methods like PUT or DELETE will not be used anymore.
My commands will be expressive domain commands and queries like:
- AddUserToGroup POST
- AddNewCustomer POST
- RemoveProductFromCustomer POST
- DeleteProduct POST
- GetCustomersByFilter POST - but only because the filter object is in the body
- GetCustomerById GET
But all giants like Microsoft or Google or Facebook design RESTful APIs and therefore I am hesitating to go my own way.
- Whats wrong with my thinking?
- Why are all APIs that I can think of are REST Apis?
- Can you provide some book or blog article where those issues are addressed?
- What are you doing?