REST:HTTP 303被认为对异步操作有害吗?

Gil*_*ili 10 rest http-status-code-303

在研究用于异步操作的RESTful API时,我遇到了以下设计模式:

POST uri:longOperation 收益:

  • HTTP 202
  • 位置:uri:pendingOperation

GET uri:pendingOperation 收益:

  • 如果操作正在运行
    • 返回进度报告.
  • 如果操作完成
    • HTTP 303
    • 地点:uri:operationResponse

GET uri:operationResponse

  • 异步操作的响应

我发现最后一步有问题了.考虑如果异步操作使用无意义的错误代码完成会发生什么HTTP GET,例如HTTP 409 ("Conflict").

  1. HTTP 303要求指向与uri相关的响应:pendingOperation而不是uri:operationResponse
  2. HTTP 303这种方式认为有害吗?如果没有,为什么?
  3. 这是我们能做的最好的,还是有更好的方法?

小智 6

是不是需要HTTP 303指向与uri相关的响应:pendingOperation而不是uri:operationResponse?

规范没有明确说明它是必需的,但我倾向于同意你的看法.

以这种方式使用HTTP 303是否有害?如果没有,为什么?

我认为你做了303就失去了能力.虽然完成后自动重定向"很好",但它使得你没有机会提供结果的元数据,可以利用报告等...此外,许多客户端不会自动关注303,因此客户端可能需要继续操作以遵循303 Location标头.

这是我们能做的最好的,还是有更好的方法?

我倾向于建议具有GET uri:pendingOperation状态资源的返回200始终在输出"完成"时引用输出.就像是

不完整时

{
    "status" : "PENDING"
}
Run Code Online (Sandbox Code Playgroud)

何时出错

{
    "status" : "COMPLETE"
    "errors" : [
       {
          "typeId" : "OPERATION_TIMEOUT",
          "description" : " "The request was unable to complete because the systems are unresponsive".
       }
    ]
}
Run Code Online (Sandbox Code Playgroud)

什么时候成功

{
    "status" : "COMPLETE"
    "links" : {
       "result" : {
          "href" : "http://api.example.com/finished-resource/1234",
       }
    ]
}
Run Code Online (Sandbox Code Playgroud)