So... I am aware that this question could be dangerously close to being opinion based . I'm hoping is not, and that the REST standard is clear about what I'm going to ask but if it's not, I'll close it.
I have a website (in Django, with the data being stored in Postgres) with Product
(-s) and Category
(-ies) Each category
can contain several product
(-s).
So, the question is: What would be the "right" endpoint (if there's any) to GET all the categories with all the products in each?
I believe it would be clear getting all the products of a specific category. For instance, if the category ID was 24
, in order to get all its products, I would write:
http://myserver.com/api/categories/24/products
But how about ALL the categories with ALL the products in each?
Would it be http://myserver.com/api/categories/products
?
Would it be http://myserver.com/api/categories/all/products
?
Would it be better using some kind of parameter, such as http://myserver.com/api/categories?mode=all_products
?
The idea would be having a response like this (JSON format)
{
"25": [{
"id": 1,
"name": "Product 1 in category 25",
"price": 100
}, {
"id": 2,
"name": "Product 2 in category 25",
"price": 200
}],
"26": [{
"id": 3,
"name": "Product 1 in category 26",
"price": 300
}, {
"id": 4,
"name": "Product 2 in category 26",
"price": 400
}]
}
Thank you in advance.
Under REST principles, a URL identifies a resource. The following URL design patterns are considered REST best practices: URLs should include nouns, not verbs. Use plural nouns only for consistency (no singular nouns).
Relative vs Absolute. It is strongly recommended that the URLs generated by the API should be absolute URLs. The reason is mostly for ease of use by the client, so that it never has to find out the correct base URI for a resource, which is needed to interpret a relative URL.
A URI can point to at most one resource, but many URIs can point to the same resource. A many-to-one relationship between URIs and resources, if you will.
The GET /api/v2/users/{id} endpoint allows you to retrieve a specific user using their Auth0 user ID. This endpoint is immediately consistent, and as such, we recommend that you use this endpoint for: User searches run during the authentication process.
As far as REST is concerned if you are uniquely representing the resource in the url so that it is cacheable
(and abiding with HATEOAS but let's skip that part), it doesn't really matter how you structure your urls. With that said in my opinion, since you want to get all the products, your url should be something like
GET /products # to return all products with their associated categories
GET /category/24/products # to return all products belonging to a particular category
Note:- Although url structure is not exactly a part of REST, but designing url in terms of an entity/resource and an identifier does makes it easier to create RESTful APIs. Well structured urls also makes them easier to be consumed by clients.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With