在 RESTful 应用程序中,我们如何区分“动作"和“动作"?和一个 HTTP 动词(GET、POST、PUT、DELETE)? [英] In a RESTful application, how do we differentiate between an "action" and an HTTP verb (GET, POST, PUT, DELETE)?

查看:54
本文介绍了在 RESTful 应用程序中,我们如何区分“动作"和“动作"?和一个 HTTP 动词(GET、POST、PUT、DELETE)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 RESTful 应用程序中,我们如何区分动作"和 HTTP 动词(GETPOSTPUT、<代码>删除)?

In a RESTful application, how do we differentiate between an "action" and an HTTP verb (GET, POST, PUT, DELETE)?

例如,据我所知,对资源 /productsGET 请求应该返回所有产品的列表.对 /productsPOST 请求应该创建一个新产品.那么,用户如何请求用于创建产品的原始表单?我的初始响应是对同一 URI 的 GET 请求,但如上所述,它应该返回所有产品的列表 - 而不是用于创建产品的空白表单.

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 的一部分来解决的.例如,对 /products/createPOST 请求将创建一个新产品,而对 /products/create 的 GET 请求 将提供用于创建产品的空白表单.要获取所有产品的列表,可以向 /products/products/get/products/read 发出 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/42http://who.cares/product/42/specification .这个对网络服务器的查询将转换为 Product 控制器.动作名称应该通过结合请求方法和命令来创建:

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 应用程序中,我们如何区分“动作"和“动作"?和一个 HTTP 动词(GET、POST、PUT、DELETE)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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