Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST URL structure advice

Tags:

rest

http

url

I'm trying to finalise on a restful url structure for the wishlist section of a site I'm working on. It's a pretty simple model, a user can have many wishlists and each wishlist can contain many products.

Currently I have the obvious CRUD URLs to manipulate the wishlist itself :

GET account/wishlists.json
GET account/wishlists/{id}.json
POST account/wishlists.json?name=My%20Wishlist
POST account/wishlists/{id}.json?name=My%20New%20Name
DELETE account/wishlists/{id}.json

However, I don't think I know how to structure the URLs that would add/remove a product to a wishlist :(

Here are my current choices :

1) Have the product to add as part of the URL and use the HTTP verb to define my action

POST account/wishlist/{id}/product/{product_id}.json
DELETE account/wishlist/{id}/product/{product_id}.json

or

2) Have the action as part of the URL and the product id as part of the payload

POST account/wishlist/{id}/add.json?product_id={product_id}
POST account/wishlist/{id}/remove.json?product_id={product_id}

(1) is clean and, as far as I can tell it's pretty RESTful but doesn't allow things like adding multiple products easily etc.

I'm also a bit concerned about using the DELETE verb - I'm not deleting the product or the wishlist, I'm just removing one from the other.

(2) is more explicit but veers away from REST - I wouldn't be just referring to the resource in the url, I would be referring to an operation on that resource :(

Any advice on which of the above would be more correct would be very helpful! (If there's a third option that's better than mine, feel free to correct me!)

like image 995
deanWombourne Avatar asked Aug 18 '11 14:08

deanWombourne


1 Answers

(1) is the only valid approach for REST, using HTTP verbs for actions.

(2) encodes method names into the URI which is more like RPC and of course not RESTful.

Concerning your shortcomings about the first approach:

  • The DELETE verb is fine, because your resource is the item inside in the wishlist, not the item itself.
  • You can support batch requests. For instance, you might want to allow to POST a list of items to a wishlist resource resulting in mutliple adds.

PS: Prefer HTTP content negotiation (Accept and Content-Type headers) over representation formats encoded in the URI.

like image 57
b_erb Avatar answered Oct 13 '22 08:10

b_erb