user3244615
user3244615

Reputation: 430

Restful URI design

Let's say that the domain structure of anapplication is as follows:

  1. There is domain object called Department.
  2. There is a domain object called Student.
  3. There is a domain object called Paper.
  4. The relationship between Student and Department is many-to-many.
  5. A student can publish (create) a Paper for himself or for a particular Department.
  6. A student can view all the papers published by him for himself and for departments to which he belongs (the latter includes papers published by other students belonging to the same department as the given student)

Here is what I think the restful uri designs should be like

  1. Student creates (POST) a white paper for himself :
    /students/{studentid}/papers
  2. Student creates (POST) a white paper for a particular department
    /students/{studentid}/departments/{departmentid}/papers
  3. Get all student papers published by him for himself /students/{studentid}/papers/self
  4. Get all student papers published by him for himself including the papers of the departments to which he belongs /students/{studentid}/papers
  5. Similar get requests for point number 1 and 2.

The other way to arrive at the above end points would be something like (considering only points 1 and 2) :

/students/{studentid}/papers

and then pass departmentid in the request body. The application would the check for the presence of departmentId in the request. If it's not null then it will assume that this paper is being published for the given departmentid, otherwise for the student himself.

Which one of the above would be a better approach?

Upvotes: 1

Views: 98

Answers (2)

Thierry Templier
Thierry Templier

Reputation: 202138

This link could help you to design your RESTful service: https://templth.wordpress.com/2014/12/15/designing-a-web-api/.

In addition, here are my comments regarding your URLs:

  • Everything that identifies your resource should be within the resource path (for example departmentid)
  • Regarding relations, we need to identify which URLs will handle references. For example, /students/{studentid}/departments/{departmentid}/papers will allow to attach an existing paper to a department or create a new one and in addition attach it to the department
  • I don't understand this url: /students/{studentid}/papers/self especially the token self. Does self refer to the current authenticated user? If so, I think that should use a query parameter since it doesn't really correspond to a resource... In fact, you rather use query parameters for list filtering

Hope it helps you, Thierry

Upvotes: 1

user1907906
user1907906

Reputation:

Since departmentid is part of how a resources is identified, it must be part of the URL. Putting it into the request body is a violation of REST principles.

Upvotes: 1

Related Questions