Is it wrong to return 202 “Accepted” in response to HTTP GET?

后端 未结 4 1395
遇见更好的自我
遇见更好的自我 2020-12-02 16:24

I have a set of resources whose representations are lazily created. The computation to construct these representations can take anywhere from a few milliseconds to a few hou

相关标签:
4条回答
  • 2020-12-02 16:50

    We did this for a recent application, a client (custom application, not a browser) POST'ed a query and the server would return 202 with a URI to the "job" being posted - the client would use that URI to poll for the result - this seems to fit nicely with what was being done.

    The most important thing here is anyway to document how your service/API works, and what a response of 202 means.

    0 讨论(0)
  • 2020-12-02 17:02

    If it's for a well-defined and -documented API, 202 sounds exactly right for what's happening.

    If it's for the public Internet, I would be too worried about client compatibility. I've seen so many if (status == 200) hard-coded.... In that case, I would return a 200.

    Also, the RFC makes no indication that using 202 for a GET request is wrong, while it makes clear distinctions in other code descriptions (e.g. 200).

    The request has been accepted for processing, but the processing has not been completed.

    0 讨论(0)
  • 2020-12-02 17:09

    From what I can recall - GET is supposed to return a resource without modifying the server. Maybe activity will be logged or what have you, but the request should be rerunnable with the same result.

    POST on the other hand is a request to change the state of something on the server. Insert a record, delete a record, run a job, something like that. 202 would be appropriate for a POST that returned but isn't finished, but not really a GET request.

    It's all very puritan and not well practiced in the wild, so you're probably safe by returning 202. GET should return 200. POST can return 200 if it finished or 202 if it's not done.

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

    0 讨论(0)
  • 2020-12-02 17:10

    In case of a resource that is supposed to have a representation of an entity that is clearly specified by an ID (as opposed to a "factory" resource, as described in the question), I recommend staying with the GET method and, in a situation when the entity/representation is not available because of lazy-creation or any other temporary situation, use the 503 Service Unavailable response code that is more appropriate and was actually designed for situations like this one.

    Reasoning for this can be found in the RFCs for HTTP itself (please verify the description of the 503 response code), as well as on numerous other resources.

    Please compare with HTTP status code for temporarily unavailable pages. Although that question is about a different use case, it actually relates to the exact same feature of HTTP.

    0 讨论(0)
提交回复
热议问题