Tom
Tom

Reputation: 16236

Restful API design with composite DTOs

I get the simple Restful design following one type of object:

GET /users
GET /users/123
POST /users/new
POST /users/1/edit (or PUT)
POST /users/1/delete (or DELETE)
...

Follow a Relationship from a type of object to another:

GET /user/123/company
GET /user/123/roles
POST /user/123/roles/new
…

What about a composite DTO mixing multiple objects in it? For example:

//Listing all users with their companies and primary role
GET /usersWithCompaniesAndPrimaryRoles 

//List all companies with users and roles count in each company
GET /companiesWithUsersAndRolesCount

In this case, my API link doesn’t look very clean or Restful to me anymore? I am wondering how I should structure the CRUD of these composite DTOs in a Restful way? Please advise me, or link me to where I can learn how to do that?

Thank you very much.

Upvotes: 1

Views: 342

Answers (1)

Liel
Liel

Reputation: 2437

A Restful design should have such API paths. Instead, you should use the GET /users URI, and provide query parameters. for example:

GET /users?with_companies=yes

In the Restful, you should not think about URIs as API commands. Try, instead to think about URIs as entities, that you can Get, Insert, change, and delete. There is no room for other "commands".

That is why, for example, There shouldn't be POST /users/new API path.
instead, just post to /users.

Another possible RESTfull way is to create a "Query Resource" object using POST, returning "Query Resource ID Number", which will later be used in a separated GET request. By that you are creating saved queries which can also be efficient for re-querying as well.

Upvotes: 1

Related Questions