如何在 REST 中为动作建模? [英] How to model an action in REST?
问题描述
假设我有 Task 和 TimePeriod.TimePeriod 表示我使用 start_time 和 end_time 字段处理任务的时间.
Suppose I have Task and TimePeriod. TimePeriod represents the time I have worked on the task using start_time and end_time fields.
我希望 Task 支持启动/停止任务.Start 将使用当前时间创建一个新的 TimePeriod.Stop 会将 end_time 添加到最后一个 TimePeriod.
I would like for Task to support Starting/Stopping the task. Start will create a new TimePeriod with the current time. Stop will add end_time to the last TimePeriod.
我认为 REST URL 将是这样的:PUT/tasks/1011?do=startPUT/tasks/1011?do=stop
I was thinking the REST URL will be like this: PUT /tasks/1011?do=start PUT /tasks/1011?do=stop
或者也许PUT/tasks/1011/startPUT/tasks/1011/stop
or maybe PUT /tasks/1011/start PUT /tasks/1011/stop
制作这种 REST 风格的正确方法是什么?
What is the proper way to make this REST style?
推荐答案
这是一种替代方案.像这样检索任务,
Here is one alternative. Retrieve the task like this,
GET /task/1011
然后开始任务,像这样POST它,
Then to start the task, POST it like this,
POST /ActiveTasks
并结束任务,
POST /InactiveTasks
实际上,如果您对任务资源使用以下表示形式,则 URI 的外观并不重要:
In reality it doesn't matter what the URIs look like if you use the following representation for a task resource:
<Task>
<Description>Do some stuff</Description>
<Status>Active</Status>
<Link rel="end" Method="POST" href="/InactiveTasks"/>
</Task>
或
<Task>
<Description>Do some stuff</Description>
<Status>InActive</Status>
<Link rel="start" Method="POST" href="/ActiveTasks"/>
</Task>
请注意,任何时候只有一个链接可用,具体取决于任务当前是否处于活动状态
Note that only one of the links will be available at any one time, depending on whether the task is currently active
这篇关于如何在 REST 中为动作建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!