http - 哪个 HTTP 状态代码表示 "Not Ready Yet, Try Again Later"?

标签 http rest language-agnostic batch-processing http-status-codes

<分区>

我正在开发一个 RESTful API,其中 http://server/thingyapi/thingyblob/1234 返回与项目 #1234 关联的文件(又名“blob”)以供下载。但是可以在生成文件之前发出请求。它肯定在以后可用。

服务器中有一个批处理过程可以生成所有的 blob。项目 1234 已经存在,并且它的数据(blob 除外)已经可用。服务器还没有生成 1234 的 blob。

我不想返回404;那是为了不存在的东西。这将存在,但尚未生成。有点像正在“处理”的 YouTube 视频。我认为重定向代码也不合适;没有“其他”URL 可供尝试。

在这种情况下返回的正确 HTTP 状态代码是什么?

最佳答案

“问题”,例如,在服务器端:客户端提出了一个格式正确的请求,但服务器不能满足它。所以我倾向于“服务器错误”,5xx 状态代码。

Quot RFC 7231 (当前的 HTTP 标准,强调已添加):

The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method. Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.

注意

  • “错误的无法执行请求”:尽管它们的标题是“服务器错误”,但它们不仅仅是针对服务器错误。
  • 临时或永久”:这些代码适用于暂时不可用的资源,例如您的资源。

在可用代码中,我会说 503, "Service Unavailable"是最合适的:

The 503 (Service Unavailable) status code indicates that the server is currently unable to handle the request due to a temporary overload or scheduled maintenance, which will likely be alleviated after some delay. The server MAY send a Retry-After header field... to suggest an appropriate amount of time for the client to wait before retrying the request.

注意:

  • “可能会在一些延迟后得到缓解”:对于您的情况是正确的。
  • “临时过载”:对于您的情况而言并非迂腐地正确。但是,有人可能会争辩说,如果您的服务器速度快得多,那么当客户端发出请求时批处理就已经完成了,所以它是一种“重载”:客户端正在请求资源速度快于服务器可以提供的速度。
  • 重试适合您的服务,因此您的回复应该包含一个Retry-After 值。您可以提供下一次执行批处理的预计完成时间或批处理的执行间隔作为值。

定义自己的 5xx 状态码(例如 591),尽管 permitted , 会有错误的语义:

a client MUST understand the class of any status code, as indicated by the first digit, and treat an unrecognized status code as being equivalent to the x00 status code of that class

客户端会将您自己的状态码视为500, "Internal Server Error" ,这是不对的。

关于http - 哪个 HTTP 状态代码表示 "Not Ready Yet, Try Again Later"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9794696/

相关文章:

c++ - C++ 的 HTTP 和 HTTPS 服务器库

node.js - POSTMAN 无法读取请求正文

algorithm - 支持快速下一个高元素和下一个低元素的数据结构是什么?

encoding - 为什么不使用base128?

使用 API 的 PHP Curl 身份验证问题

Http Binding 负载均衡器

android - 如何在 Android 中执行 HTTP Post?

java - 返回外部 ID Java RestFul WS

rest - Spring Boot REST token 授权和认证最佳实践

algorithm - 如何检查一个盒子是否适合另一个盒子(允许任何旋转)