Starving Techie Starving Techie - 5 days ago 6
reST (reStructuredText) Question

Which HTTP method to use for RESTful api for "add to cart" an existing item already in cart?

I have started designing addToCart method as HTTP POST in my RESTful API. This looks good when the client adds to cart a product first time (POST create a new entry on server). But, the same HTTP rule breaks when the client browses through the site and adds the same item again; where we should not create a new entry but only update the quantity of existing item.

Isn't using POST to update resource wrong? What is the way to implement this? or how to interpret this situation.

Note: Client/UI front which uses my api would not remember if its already there on server. Please consider any ecommerce application's addtocart as example.

jwg jwg

I think the difficulties here are already present in how you add the original item to the cart.

When you add an item to cart, are you creating a new object (the item)? Or are you modifying an existing item (the cart)? To me it makes more sense to say the latter. POSTing an item seems like it should be reserved for a different situation, when you add a new item to your store.

Conceptually, the cart is then like a vector of all the items in your store, associated with a number (0 for almost all of them). Adding something to the cart means incrementing this number for one of the items, regardless of whether it is already more than 0.