如何取消 REST api 请求? [英] How to Cancel a REST api request?
问题描述
我使用基于 REST API 的系统,其中有些请求需要很长时间才能完成.我想给用户一个取消请求的选项.
I use REST API based system, in which there are some requests that take long time to complete. I want to give user an option to cancel the request.
推荐答案
一、支持
POST /requests
将返回对请求状态的引用
which will return a reference to the status of the request
{
"id": 1234,
"self"": "/requests/1234"
"status": "Running"
}
然后添加对
PUT /requests/1234
{
"status": "Canceled:"
}
如果请求尚未完成,这将允许客户端取消请求.如果请求是创建某种其他类型的资源,则不使用 POST/requests
,而是执行 POST/myResource
,但仍返回带有指向/requests 的指针的状态对象在回复中.
That will let clients cancel a request if it hasn't finished yet. If the request is to create some other kind of resource, then instead of POST /requests
, do POST /myResource
, but still return the status object with the pointer to /requests in the response.
然后客户端可以轮询 /requests
以查看请求何时完成.
Clients can then poll /requests
to see when the request is complete.
这篇关于如何取消 REST api 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!