Expose single REST endpoint (GET) which returns a parent object and all the child objects associated with it

Viewed 450

Is it correct to implement one REST endpoint (GET) to expose parent and set of child objects together?

e.g:

parent { name: parent, id: 123, Children:{ { childName:1 }, { childName:2 } } }

I am thinking two approaches:

  1. Implement one API named "parentWithchildren" (GET: /parentWithchildren/)
  2. Just endpoint for parent, and it will return a Parent object with set of child objects (GET: parent/)
1 Answers

If we follow the rest api principles and we assume that your resource is Parent and a Parent can have children. Lets say each child is identifiable by Id as well as the Parent then your rest endpoint will look like:

get /parent/{parentId}
get /parent/{parentId}/child/{childId}

Now you can potentialy create crude version of your service that is also useful if your children dont have ID than it will look like:

get /parent/{parentId}?fetchChildren=true

Implementing an endpoint as /parentWithchildren is more of a RPC style of programming, not very rest like.

Related