Search code examples
restrestful-url

Rest API chained parameters in URL


I am currently designing a REST API. I have got a Group Entity and an Event Entity. One Event belongs to exactly one Group, but one Group can have multiple Events.

Groups are accessed via

/groups/group/{groupId}

I am not sure about where to put the Event endpoint:

/groups/group/{groupId}/events/event/{eventId}

or just

/events/event/{eventId}

Posting new Events into a Group is easier with the upper, whereas just getting data about one Event is easier with the lower approach.
Which of them should I use, or should I even "mix" them? (one for GET, one for POST)


Solution

  • First of all your REST basic endpoints should be like this:

    • [POST] add group /groups
    • [GET] get all groups /groups
    • [GET] fetch/get a group by id `/groups/{id}
    • [PUT]update/modify a group by id `/groups/{id}
    • [DELETE] delete a group by id `/groups/{id}

    • [POST] add event /events
    • [GET] get all events /events
    • [GET] fetch/get a event by id `/events/{id}
    • [PUT]update/modify a event by id `/events/{id}
    • [DELETE] delete a event by id `/events/{id}

    So now come on you want to fetch an event of the specific group by id like you tried here /groups/group/{groupId}/events/event/{eventId} right?

    you can fetch all events of a group like this :

    • /events?group_id={groupId} like this here you can implement.
    • and the same for vice-varsa you can fetch all groups those who have the specific event by this /groups?event_id={event_Id}
    • Now if you want to fetch specific group and specific event so you can do by /groups/{id}?event={eventId} that 3rd route or endpoint of Group's REST above and same for vice-varsa.