gonzalomelov
gonzalomelov

Reputation: 981

REST URI - Best approach

get /cars -> Get all cars
get /cars/1 -> Get car with id 1
post /cars -> Create car
post /cars/1/bookmark -> The authenticated user bookmarks the car with id 1

For the authenticated user to get his bookmarked cars we can have:

get /cars/bookmarked

or

get /users/me/cars/bookmarked

Which one is the preferred?

Upvotes: 0

Views: 65

Answers (2)

Joshua Sprague
Joshua Sprague

Reputation: 121

GET /cars

often referred to as the "collection". this should return all cars that the consumer has visibility to. it is common to include query parameters in order to manipulate the response such as GET /cars?type=sports&color=red

when you have a "collection", it is a natural extension to also provide a specific "document" (or member of the collection)

GET /cars/{id}

this usually will not have query parameters to manipulate the response since the request was for a specific and known document/resource.

in order to create a new resource, you would use post

POST /cars

and you should receive a http code 201

in order to update an existing resource, generally you would use PUT, but there is a trend to use POST for updates as well as creations http://www.thoughtworks.com/radar/techniques

to try to address your question, i believe you either need to treat bookmark as it's own resource and allow users to POST/PUT to the bookmark resource with links/references to cars, or include a bookmark aspect to the cars resource and allow users to POST/PUT to it.

Upvotes: 0

user1431072
user1431072

Reputation: 1442

There isn't a "preferred" standard and this question has nothing to do with Rest. Rest is not about how to structure your URIs. Rest is an architectural style whose centerpiece is Hypermedia-as-the-engine-of-application-state. In real Rest, clients don't know about any URIs other than a single entry point URI.

With that said, after I found out what I just explained above, and after many months of wondering what the perfect URI structre for my APIs was, I was also able to realize that it is best NOT to assume that there is a "preferred" URI structure for APIs. Sure, there are some pros and cons with certain structures, but overall, you will get different responses from different people. Hardly anybody agrees on a "preferred" URI structure for HTTP based APIs. I say just choose a URI structure that you like and move on.

Upvotes: 4

Related Questions