gpt4 book ai didi

REST:针对慢速操作的响应式 UI?

转载 作者:行者123 更新时间:2023-12-03 10:05:26 25 4
gpt4 key购买 nike

我需要帮助为带有进度条的应用程序创建 RESTful 设计。

想象一个应用程序,其中一个资源需要很长时间(1 分钟以上)来响应 HTTP GET(在我的例子中,我正在扫描网络以查找设备)。我希望客户端显示一个进度条,指示 GET 操作需要多长时间,但为了使其工作,服务器需要为他们提供操作的时间估计。

给定一个缓慢的操作:
HTTP GET /devices
什么是为它提供时间估计的 RESTful 方式?我不认为我可以使用:
HTTP HEAD /devices
因为 HEAD 应该返回与 GET 相同的值减去主体,这(我认为)意味着我将不得不运行我试图避免的相同长的操作。有任何想法吗?

最佳答案

再想一想,我想我会选择 incremental response 。根据 RESTful Web Services 异步操作最好用 HTTP 202 表示。

  • 客户端发送 HTTP POST /devices
  • 服务器响应 HTTP 202 Accepted. Location: /queues/32194532
  • 引用这本书:

    The asynchronous operation is now in progress, and the client can make GET requests to that URI to see how it’s going— that is, to get the current state of the “job” resource. Once the operation is complete, any results will become available as a representation of this resource. Once the client is done reading the results it can DELETE the job resource. The client may even be able to cancel the operation by DELETEing its job prematurely.

  • 引用 https://stackoverflow.com/a/5081246/14731 :当请求的进程仍然挂起时,作业应该返回 200 OK。响应应该描述进程的挂起状态。
  • 引用 https://stackoverflow.com/a/5081246/14731 :处理完成后,作业应返回 201 CreatedGET/PUT/POST 的响应应包含请求/创建/更新资源的位置。
  • 引用这本书:

    There’s one wrinkle. Because every request to start an asynchronous operation makes the server create a new resource (if only a transient one), such requests are neither safe nor idempotent. This means you can’t spawn asynchronous operations with GET, DELETE, or (usually) PUT. The only HTTP method you can use and still respect the uniform interface is POST.

  • 关于REST:针对慢速操作的响应式 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8388923/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com