[英]Using HTTP DELETE method to cancel an action in progress
Background: 背景:
I provide a programming interface for people who wish to place orders using my site. 我为希望使用我的网站下订单的人提供编程界面。
They can use GET
and POST
methods on /api/v1/orders
to place an order and to view list of all orders placed by them. 他们可以在
/api/v1/orders
上使用GET
和POST
方法/api/v1/orders
订单并查看他们下的所有订单的清单。 They can also use the GET
metod on /api/v1/orders/<order_id>
in order to view specific details of one order. 他们还可以使用
/api/v1/orders/<order_id>
上的GET
方法来查看一个订单的具体细节。
There is a need to provide a way to cancel an order, however the records themselves need to be kept. 需要提供取消订单的方法,但是需要保留记录本身。
I would like to get feedback from more seasoned developers on whether it would be a sane decision to: 我想得到更多经验丰富的开发人员的反馈意见,了解是否能做出明智的决定:
a) implement the DELETE
verb on /api/v1/current_orders/<order_id>
, which would delete it from the list of "current" orders (by marking it as cancelled). a)在
/api/v1/current_orders/<order_id>
上实现DELETE
动词,这将从“当前”命令列表中删除它(通过将其标记为已取消)。 The downside is that it will make use of a different noun, which may be confusing. 缺点是它将使用不同的名词,这可能会令人困惑。
b) implement the DELETE
verb on /api/v1/orders/<order_id>
with the same functionality as in a). b)在
/api/v1/orders/<order_id>
上实现DELETE
动词,其功能与a)相同。 This is somewhat misleading, as the entity will not really be deleted, and the consumer should be aware of that. 这有点误导,因为实体不会真正被删除,消费者应该意识到这一点。
c) implement the POST
verb on /api/v1/cancellations/<order_id>
(or POST
on /api/v1/cancellations
with the order_id in the JSON payload). c)在
/api/v1/cancellations/<order_id>
上实现POST
动词(或使用JSON有效载荷中的order_id对/api/v1/cancellations
进行POST
)。 This seems to be less than ideal because a resource will not be created as a result of that request. 这似乎不太理想,因为该请求不会创建资源。 However, the consequences of using this endpoint seem to be clearer.
但是,使用此端点的后果似乎更清晰。
d) ...? d) ......?
Question: 题:
I am aware that there is not always a "perfect" solution to designing endpoints for a REST API, but keeping in mind the need for clarity and intuitiveness and with a high regard for best practices , which of the options is "optimal" ? 我知道,为REST API设计端点并不总是一个“完美”的解决方案,但要记住需要清晰和直观 ,并且要高度重视最佳实践 , 哪些选项是“最优的” ?
What about PATCH
verb on /api/v1/orders/<order_id>
indicating that it is cancelled? /api/v1/orders/<order_id>
上的PATCH
动词如何表示它被取消了?
HTTP PATCH : Allows to do partial modifications in an entity. HTTP PATCH :允许在实体中进行部分修改。 While POST creates a new one, and PUT replaces an existing one, PATCH just updates the properties you are sending, leaves the rest at they are.
当POST创建一个新的,并且PUT替换现有的一个时,PATCH只更新你发送的属性,剩下的就是它们。
You would need only to send something like { isCancelled:true}
as HTTP PATCH
, then your code would update the entity and take action like cancel any outstanding work. 您只需要将
{ isCancelled:true}
作为HTTP PATCH
,然后您的代码将更新实体并采取行动,例如取消任何未完成的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.