基于REST的API时在同一个URI多个动作 [英] RESTful APIs when multiple actions on the same URI

查看:192
本文介绍了基于REST的API时在同一个URI多个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,据我所知,四样的方法在REST的API一起使用:

So far as I know, four kind of methods are used in RESTful APIs:

GET 获取该资源。结果
上传作为更新的资源。结果
PUT 创建或替代资源。结果
删除删除该资源。

GET for getting the resource.
POST for updating the resource.
PUT for creating or substituting the resource.
DELETE for deleting the resource.

假设我们有一个名为资源苹果,我们可以更新它在几个方面。例如,削减它,分析它,还是让它苹果汁。结果
这三个不同的升级行动,采取不同的参数,以及他们的API,常见的部分是:

Assume we have a resource named apple, and we can 'update' it in several ways. For example, pare it, slice it, or make it apple juice.
Each of these three different updating actions takes different arguments, and of their APIs, the common part will be:

POST /apple HTTP/1.1
Host: www.example.com

<different combination of arguments>

在这种情况下,三个API共享相同的URI和相同的请求的方法,其中唯一的区别是变量。我想,这迫使后端准备好接受并集的这些参数,并区分哪些动作被实际要求,后端需要检查的参数的组合。它是如此多的复杂和不优雅。

In this situation, three APIs share the same URI and the same request method, the only differences of them are arguments. I think this forces the backend to be ready for accepting the union set of those arguments, and to distinguish which action is actually requested, the backend need to check out the combination of the arguments. It's so much complicated and not graceful.

所以我的问题是:结果
在这种情况下,苹果如何制定出一个优雅的一套RESTful API中这使得后端轻松地用它处理的。

So my question is:
In this apple cases, how to work out an elegant set of RESTful APIs which make the backend easily handle with it.

推荐答案

首先,尽量避免混为一谈HTTP方法CRUD操作。我相信这是混乱的REST的主要来源。 HTTP方法没有转换到CRUD操作干净利落这样。我有一个详细的解答在这里:

First of all, try to avoid conflating HTTP methods to CRUD operations. I believe that's the main source of confusion in REST. HTTP methods don't translate to CRUD operations cleanly like that. I have a detailed answer here:

S3 REST API和POST方法

在短。


  • POST是用于并非由HTTP规范的操作方法,和科目有效载荷到目标URI。

  • PUT是用来完全取代在present URI资源和主题,有效载荷服务本身。

  • PATCH是局部幂等的更新,与当前和理想状态之间的差异。

  • 删除用于删除该资源。

  • GET用于检索资源。

现在,在后端侧,再想想REST资源,更像是一个状态机,在那里你可以使用的方法来强制转型,而不是方法的对象。这样,你专注于资源本身的执行,而不是与协议的交互。例如,你可以直截了当地从方法的有效载荷改变一个对象的属性,然后有一个这就是所谓的检测所需要的传输方式。

Now, on the backend side, try to think of REST resources more like a state machine where you can use the methods to force a transition rather than an object with methods. That way you focus the implementation on the resource itself, not on the interaction with the protocol. For instance, you may change an object's attributes straightforwardly from the method's payload, and then have a method that's called to detect what transition is needed.

例如,你可能会认为苹果的具有三种状态,整体而言,相比,切片和果汁。通过使用这些方法的行为规范状态之间转变。

For instance, you may think of an apple as having three states, whole, pared, sliced and juiced. You transition between states by using the standardized behavior of the methods.

例如:

GET /apple 

{"state": "whole",
 "self": "/apple"}

然后,你要切它。你可以这样做:

Then you want to slice it. You may do something like:

PUT /apple

{"state": "sliced"}

或者你可以这样做:

Or you may do something like:

PATCH /apple

{"from_state": "whole", "to_state": "sliced"}

甚至是这样的:

POST /apple

{"transition": "slice"}

的想法是,实现可以是通用的不够,你不必担心资源耦合到HTT​​P方法太多了。

The idea is that the implementations can be generic enough that you don't have to worry too much about coupling the resource to the HTTP methods.


  • 的PUT版本是幂等的,所以你的客户可以选择时,他们需要幂等使用它。

  • 补丁版本保证了客户知道目前​​的状态,并试图有效的过渡。

  • POST版本是你想要的最灵活的,你可以做任何事情,但它需要进行详细的记录。你不能简单地认为你的客户会知道方法如何工作的。

只要你的资源的执行了解到,当 apple.state 更改为其他东西就应该检测发生的变化并进行适当的转换,你完全从协议脱钩。不要紧,用什么方法。

As long as your implementation of the resource understands that when apple.state is changed to something else it should detect what change occurred and perform the adequate transition, you are completely decoupled from the protocol. It doesn't matter what method was used.

我相信这是最优雅的解决方案,并让一切更容易从后端侧处理。你可以不用太担心该协议实施的对象。只要对象可以状态之间进行转换,它们可以由能实现这些转换任何协议使用。

I believe this is the most elegant solution, and makes everything easier to handle from the backend side. You can implement your objects without worrying too much about the protocol. As long as the objects can be transitioned between states, they can be used by any protocol that can effect those transitions.

这篇关于基于REST的API时在同一个URI多个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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