Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful API - different types of children resources

I'm dealing with tree-structred resources. Every item resource has children. Each child type may be content resource or subject resource - which is determind by the type_id. (There could be more children types in the future).

What URI should be used for creating a new child for an item?

POST /api/items/<item_id>/children

(deliver the type_id via the JSON)

OR:

POST /api/items/<item_id>/children/contents
POST /api/items/<item_id>/children/subjects

OR: redirection according to the type_id to:

POST /api/contents
POST /api/subjects

And then using the GUID of the new resource for creating the hierarchy connection.

Thanks!

like image 992
ozking Avatar asked Dec 30 '25 20:12

ozking


1 Answers

If your children have an attribute named type which can be subjects or contents, you can treat that as any other attribute, e.g. a gender that can be male or female.

Ideally, you would create a new child with

POST /api/items/<item_id>/children

{
  "some_value": 50
  "type": "subject",
}

or

POST /api/items/<item_id>/children

{
  "some_value": 134
  "type": "content",
}

No need to make a confusing endpoint for a simple attribute. If you'd do this for the type attribute, you might as well do it for all the other attributes as well, leading to a great many more endpoints pointing to basically the same resources, that is not something you want.

Later you can fetch them by type, for example getting all subjects would be

GET /api/items/<item_id>/children?type=subject
like image 102
Tim Avatar answered Jan 02 '26 13:01

Tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!