行动"在RESTful应用程序中,我们如何在&QUOT区分;和HTTP动词(GET,POST,PUT,DELETE)? [英] In a RESTful application, how do we differentiate between an "action" and an HTTP verb (GET, POST, PUT, DELETE)?
问题描述
在RESTful应用程序,我们怎么一个行动和HTTP动词(区分 GET
, POST
, PUT
,删除
)?
In a RESTful application, how do we differentiate between an "action" and an HTTP verb (GET
, POST
, PUT
, DELETE
)?
例如,按照我的理解,一个 GET
请求资源 /产品
应该返回列表所有产品。 A POST
要求 /产品
应该创建一个新的产品。那么,如何做用户要求用来创建产品的原始形式?我的第一反应会是一个 GET
请求相同的URI,但正如上文提到的,应返回的所有产品的列表 - 不是一个空白表单创建产品。
For example, as I understand it, a GET
request to the resource /products
should return a list of all products. A POST
request to /products
should create a new product. How, then, does the user request the original form which is used to create the product? My initial response would have been a GET
request to the same URI, but as mentioned above, that should return a list of all products - not a blank form for creating a product.
在我研究大多数框架,这个问题是通过URI的行动的一部分解决。例如, POST
要求 /产品/创建
将创建一个新的产品,而 GET
要求 /产品/创建
将给予空白表格创建产品。要获取所有产品清单将是一个 GET
要求为 /产品
或 /产品/获取
, /产品/读
等,取决于问题的框架。这种方法解决了上面的模糊性,但它与我所了解的传统REST设计冲突。
In most frameworks I've researched, this problem is solved by making the "action" part of the URI. For example, a POST
request to /products/create
would create a new product, whereas a GET
request to /products/create
would give the blank form for creating a product. To get a list of all products would be a GET
request to either /products
or /products/get
, /products/read
, etc. depending on the framework in question. This approach resolves the ambiguity above, but it conflicts with what I've read about traditional REST design.
推荐答案
恕我直言,最好的选择就是让请求方法控制器的行动的一部分。
IMHO, the best option is to make the request method a part of controller's action.
假设你正在访问 HTTP://who.cares/product/42
或 HTTP://who.cares/product/42 /规格
。该查询到网络服务器会翻译为产品
控制器。动作名称应结合创建请求方法和命令:
Lets say you are accessing http://who.cares/product/42
or http://who.cares/product/42/specification
. This query to webserver would translate as Product
controller. The actions name should be created by combining request method and command:
DELETE "http://who.cares/product/42"
controller: "Product",
action: "deleteProduct()"
GET "http://who.cares/product/42/details"
controller: "Product",
action: "getDetails()"
POST "http://who.cares/product/42/review"
controller: "Product",
action: "postReview()"
GET "http://who.cares/products/
controller: "Products",
action: "getProducts()"
POST "http://who.cares/products/
controller: "Products",
action: "postProducts()"
这篇关于行动"在RESTful应用程序中,我们如何在&QUOT区分;和HTTP动词(GET,POST,PUT,DELETE)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!