rest

REST API resource creation


I'm developing REST API. There are 'Users' and 'Tasks' resources and each user is able to create 'UserTask'. So, what kind of URL is in line with REST API standards and conventions? Which approach is more logical In the end?

post: users/{id}/tasks

or

post: tasks/users/{id}

does it make sense to have both?

I have read lots of topics about it but found nothing that could be helpful in this particular case.


Solution

  • If a task belongs to a particular user, I would go for:

    POST /users/{user-id}/tasks
    

    To get a representation of a particular task, you could use:

    GET /users/{user-id}/tasks/{task-id}