Suppose I have Project and Task entities for a to-do list. If Tasks are required to be in a Project, the Task routes would probably look like this:
GET /projects/{projectId}/tasks returns all tasks for the specified project
POST /projects/{projectId}/tasks creates a new task in the specified project
But it's not quite as clear (to me) what to do if a Task can be free-standing (not part of a Project). I see a couple of options:
Have two separate routes: one for creating a project-bound task, and one for a free-standing task
POST /projects/{projectId}/tasks creates a new task in the specified project
POST /tasks creates a new free-standing task
This seems...ok. But I don't really like the idea of having to conditionally select an API route on a task-by-task basis.
Have single route and specify an optional projectId in the payload or query params
POST /tasks creates a new task
GET /tasks?projectId={projectId} gets all tasks within the specified project
GET /tasks/{taskId} gets the specified task, regardless of whether it's in a project
This seems better. I like the uniformity of having a single endpoint that handles all of this. With that approach, though, there are two ways to get a task by Id:
GET /tasks/{taskId}
GET /tasks?id=taskId
I get the sense that the first option is a bit more standard, but the second aligns better with other types of filtering one might do (by project, by name, etc.). Is it better to support both, or force one or the other?
Are any of these approaches more generally accepted? Are there other options that I haven't considered?
Are any of these approaches more generally accepted?
but it dislike you. And for the second, Do you really need to support both approaches? It does mean that you are not confident on one of them. Like if you were being politically-correct. Why?