I could use one of the following:
- GET / api / professors /: prof_id / courses
- GET / api / courses? Professor_id =: prof_id
Could you. Here are a few things to consider:
Machines (in particular, REST clients) should treat URIs as an opaque thing; the closest thing to them that they have ever come to understand their value is permission.
But people, looking at the HTTP traffic log, do not handle the URIs opaquely - we are actually trying to figure out the context of what is happening. Staying on the path of a poor bastard trying to spot an error is a good feature for URI design.
This is also a useful property for your URI design. A URI designed from a few simple consistent principles will be much easier to work with than arbitrary.
There is a great overview of the path segment and request for Programmers
https://softwareengineering.stackexchange.com/questions/270898/designing-a-rest-api-by-uri-vs-query-string/285724#285724
Of course, if you have two different URIs, then both "follow the rules", then the rules do not really help in choosing.
Support for multiple identifiers is a valid option. It is perfectly reasonable that there can be more than one way to get a specific idea. For example, these resources
/questions/38470258/answers/first /questions/38470258/answers/accepted /questions/38470258/answers/top
can return representations of the same "answer".
On the other hand, the choice adds complexity. It may or may not be a good idea to offer your customers more than one way to do something. "Don't make me think!"
On the other / other side, api with a bunch of "general" principles that carry a bunch of arbitrary exceptions is not as easy to use as one with consistent principles and some duplication (citation needed).
The concept of a "canonical" URI, which is important in SEO, has a counterpart in the API world. Mark Seemann has an article on his own links that covers the basics.
You can also consider what methods the resource supports, and whether the design offers these assumptions. For example, POST to modify a collection is a common idiom. Therefore, if your URI looks like a collection
POST /api/professors/:prof_id/courses
Then, customers are more likely to create a relationship between the resource and the supported methods.
POST /api/courses?professor_id=:prof_id
There is nothing "wrong" in this, but it is not so arbitrary.
GET / api / courses? where = {Professor_id: "teacher45", year: 2016} & order = {attr: "topic", sort: "asc"}
I have never seen anyone do it this way, although, I wonder, I'm doing something stupid.
I don't either, but syntactically this looks a bit like GraphQL . I see no reason why you could not submit the request in this way. This would make sense to me as one description of the request, rather than breaking it down into several parts. And of course, this should be encoded in a URL, etc.
But I would not want to go crazy with this right, if you really do not need to provide your customers with such flexibility. Simpler constructions exist (see Roman answer)