REST URL结构建议 [英] REST URL structure advice

查看:111
本文介绍了REST 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.

目前我有明显的CRUD URL来操纵心愿单本身:

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

但是,我认为我不知道如何构建将产品添加/删除到愿望清单的URL:(

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

以下是我目前的选择:

1)让产品添加为URL的一部分并使用HTTP谓词来定义我的操作

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

2 )将行动作为U的一部分RL和产品ID作为有效负载的一部分

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)很干净,据我所知,它很漂亮但不允许轻松添加多个产品等等。

(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.

我是还有点担心使用DELETE动词 - 我没有删除产品或心愿单,我只是从另一个删除。

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)是更明确但更偏离REST - 我不会只是引用url中的资源,我将指的是对该资源的操作:(

(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!)

推荐答案

(1)是唯一有效的REST的方法,使用HTTP谓词进行操作。

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

(2)将方法名称编码为URI,更像RPC,当然不是RESTful。

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

关于第一种方法的缺点:

Concerning your shortcomings about the first approach:


  • DELETE 动词很好,因为您的资源是心愿单中的项目,而不是项目本身。

  • 您可以支持批量请求。例如,您可能希望允许 POST 一个愿望清单资源的项目列表,从而导致多个添加。

  • 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:首选HTTP内容协商(Accept和Content-Type标头),而不是URI中编码的表示格式。

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

这篇关于REST URL结构建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆