Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API sub resources

Creating my first REST API and I have a question. I have a resource venue:

/venues

each venue has guests:

/venues/1/guests

This is where I am confused about. If I want to update a guest resource, is it better to do this:

POST /venue/1/guests/1/

or

POST /guests/1

The second option is shorter and since guests ids are unique, the 2nd one works just fine. But first is more explicit.

So I am stuck on which route to take from here.

Thanks

like image 261
0xSina Avatar asked Jan 28 '26 08:01

0xSina


2 Answers

Both are pretty much the right approaches although, if you guarantee the clients (client developers) that the guest ids are always going to be unique regardless of the venue they visit, I will go with

POST /guests/{guestId}

I will normally use this:

POST /venues/{venueId}/guests/{guestId}

when I have to change the status of that guest for that given venue. For example, Guest John Doe with id 1 has RSVP'ed "yes" for the venue with venueId 1 then I will POST the data to the following url (this is just an example where it is assumed that the only thing you can add/update for a given guest for a given venue is the RSVP data):

POST /venues/1/guests/1
request body: {"RSVP", true}

but if I want to update John Doe's name to John Foo, I will probably do

PUT /guests/1
request body: {"firstName":"John","lastName":"Foo"}

I hope that helps!

like image 119
shahshi15 Avatar answered Jan 30 '26 22:01

shahshi15


You can think of you having two RESTful resources to manage

  • A) Venues and
  • B) Guests

These resources can be independently managed using POST/GET/DELETE/PUT (CRUD operations)

POST /venues/
GET /venues/
POST /guests/
GET /guests/

Also you can use CRUD to map one resource to another like

POST /venues/[venue-id]/guests/[guest-id]/
like image 40
Ming Chan Avatar answered Jan 30 '26 22:01

Ming Chan



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!