如何实现 RESTful 进度指示器? [英] How can I implement a RESTful Progress Indicator?

查看:43
本文介绍了如何实现 RESTful 进度指示器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 API 是 RESTful

I want my API to be be RESTful

假设我已经使用 POST 启动了一个长时间运行的任务,现在想要了解进度?

Let say I have started a long running Task with POST and now want to be informed about the progress?

执行此操作的惯用 REST 方法是什么?

What is the idiomatic REST way to do this?

每 10 秒用 GET 轮询一次?

Poll with GET every 10 seconds?

推荐答案

对您的回复

POST /new/long/running/task

应该包含一个 Location 标头.该标头将指向客户端可以点击以找出任务状态的端点.我建议您的回复类似于:

should include a Location header. That header will point to an endpoint the client can hit to find out the status of the task. I would suggest your response look something like:

Location: http://my.server/task-status/15
{
    "self": "/task-status/15",
    "status": "running",
    "expectedFinishedAt": <timestamp>
}

那么你的客户端就不必随意ping了,因为服务器会提示它什么时候回来检查.稍后对 /task-status/15GET 将返回更新的时间戳.这使您不必盲目轮询服务器.当然,如果服务器知道完成处理任务需要多长时间,这会更好.

Then your client doesn't have to ping arbitrarily, because the server is giving it a hint as to when to check back. Later GETs to /task-status/15 will return updated timestamps. This saves you from having to blindly poll the server. Of course, this works much better if the server has some idea as to how long it will take to finish processing the task.

这篇关于如何实现 RESTful 进度指示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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