【问题标题】:Is HTTP 303 considered harmful for asynchronous operations?HTTP 303 是否被认为对异步操作有害?
【发布时间】:2012-12-13 19:37:45
【问题描述】:

在研究用于异步操作的 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. 这是我们能做的最好的,还是有更好的方法?

【问题讨论】:

    标签: rest http-status-code-303


    【解决方案1】:

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

    spec 没有明确表示它是必需的,但我倾向于同意你的观点。

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

    我认为您通过执行 303 会失去功能。虽然完成后自动重定向“很好”,但它使您没有机会提供可以利用的结果的元数据用于报告等...此外,许多客户端不会自动关注 303,因此客户端可能需要做一些工作来关注 303 位置标头。

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

    我倾向于建议让 GET uri:pendingOperation 返回 200 和状态资源,并在“完成”时始终引用输出。类似的东西

    当不完整时

    {
        "status" : "PENDING"
    }
    

    出错时

    {
        "status" : "COMPLETE"
        "errors" : [
           {
              "typeId" : "OPERATION_TIMEOUT",
              "description" : " "The request was unable to complete because the systems are unresponsive".
           }
        ]
    }
    

    成功时

    {
        "status" : "COMPLETE"
        "links" : {
           "result" : {
              "href" : "http://api.example.com/finished-resource/1234",
           }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-28
      • 1970-01-01
      • 2015-09-09
      • 2011-12-28
      • 1970-01-01
      • 2014-11-24
      • 2010-12-22
      • 2010-11-08
      • 1970-01-01
      相关资源
      最近更新 更多