Let's say I have I have an online store with a "shopping cart" feature and I want to implement an "empty cart" link in a RESTful way.
假设我有一个带有“购物车”功能的在线商店,我想以RESTful方式实现“空车”链接。
For simplicity, let's say my resources are a Cart that contains CartItems, each of which has a Product. My URIs might be:
为简单起见,假设我的资源是一个包含CartItems的Cart,每个CartItem都有一个Product。我的URI可能是:
# add a product to the current user's Cart POST /products/product_id/cart_items/ # remove a product from the current user's Cart DELETE /cart_items/cart_item_id/
If so, what would the RESTful URI for the "empty cart" link look like?
如果是这样,“空车”链接的RESTful URI会是什么样子?
Instead, I could think of the Cart as a general-purpose holder for Actions (as described here):
相反,我可以将Cart视为Actions的通用持有者(如此处所述):
# add a product # form data contains e.g., product_id=123&action=add POST /carts/cart_id/actions/ # remove a product # action_id is the id of the action adding product 123 DELETE actions/action_id # empty cart # form data contains action=clear POST /carts/cart_id/actions/
This approach seems more complicated than it needs to be. What would be a better way?
这种方法似乎比它需要的更复杂。什么是更好的方式?
3 个解决方案
#1
Don't do the second approach. Funneling different actions
through one endpoint does not feel RESTful IMO.
不要做第二种方法。通过一个端点汇集不同的操作并不会感觉RESTful IMO。
You have DELETE /cart_items/cart_item_id/
that removes cart_item_id
from their cart. What about DELETE /cart_items/
to clear the cart itself?
你有DELETE / cart_items / cart_item_id /从他们的购物车中删除cart_item_id。 DELETE / cart_items /如何清除购物车本身?
#2
Adding an item to a cart:
将商品添加到购物车:
POST carts/{cartid}/items
Retrieving a specific item from the cart:
从购物车中检索特定商品:
GET carts/{cartid}/items/{itemid}
Deleting a specific item from the cart:
从购物车中删除特定商品:
DELETE carts/{cartid}/items/{itemid}
Getting the state of the cart:
获得购物车的状态:
GET carts/{cartid}/state
(Could return a value like 0,1 that indicates the number of items in the cart)
(可以返回一个类似0,1的值,表示购物车中的商品数量)
Emptying the cart:
清空购物车:
PUT carts/{cartid}/state?state=0
Does this look intuitive?
这看起来很直观吗?
#3
DELETE /cart_items/
is an interesting idea that has also been discussed here.
DELETE / cart_items /是一个有趣的想法,这里也讨论过。
#1
Don't do the second approach. Funneling different actions
through one endpoint does not feel RESTful IMO.
不要做第二种方法。通过一个端点汇集不同的操作并不会感觉RESTful IMO。
You have DELETE /cart_items/cart_item_id/
that removes cart_item_id
from their cart. What about DELETE /cart_items/
to clear the cart itself?
你有DELETE / cart_items / cart_item_id /从他们的购物车中删除cart_item_id。 DELETE / cart_items /如何清除购物车本身?
#2
Adding an item to a cart:
将商品添加到购物车:
POST carts/{cartid}/items
Retrieving a specific item from the cart:
从购物车中检索特定商品:
GET carts/{cartid}/items/{itemid}
Deleting a specific item from the cart:
从购物车中删除特定商品:
DELETE carts/{cartid}/items/{itemid}
Getting the state of the cart:
获得购物车的状态:
GET carts/{cartid}/state
(Could return a value like 0,1 that indicates the number of items in the cart)
(可以返回一个类似0,1的值,表示购物车中的商品数量)
Emptying the cart:
清空购物车:
PUT carts/{cartid}/state?state=0
Does this look intuitive?
这看起来很直观吗?
#3
DELETE /cart_items/
is an interesting idea that has also been discussed here.
DELETE / cart_items /是一个有趣的想法,这里也讨论过。