SandeliusME
SandeliusME

Reputation: 822

REST API URL best practice

Let's say I have two models. Project and Task. A task is must belongs to a project.

Which of the following URL endpoints is best practice?

  1. GET https://api.myapp.com/projects/:project_id/tasks/:task_id
  2. GET https://api.myapp.com/tasks/:task_id?project_id=:project_id

For filtering result I always use query params but I wondering what SHOULD we do with required params?

Upvotes: 2

Views: 7073

Answers (1)

Lucas Leite
Lucas Leite

Reputation: 467

According to the REST best practices, a resource member should be in the url. In your case this means:

https://api.myapp.com/projects/:project_id/tasks/:task_id

Query parameters should be used as filters. For example to get all tasks whose name begins with "TEST"/

https://api.myapp.com/projects/:project_id/tasks/:task_id?nameStartsWith=TEST

You can read more about it in here: http://www.restapitutorial.com/resources.html Download the PDF and take a look at page 14 - Resource naming.

Upvotes: 6

Related Questions