ReST: http 204 status code for polling for a resource after a 201 Created
I have a request (POST) which creates a resource. This resource takes a long time to create (up to hours), but its id is created immediately.
It seems to me the most appropriate flow is:
POST /thing - response is 201 Created with "URI for the resource given by a Location header field" (as per http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
Start polling GET /thing/id to which the response should be:
As long as resource has not been prepared - 204 No Content
Once the resource is ready - 200 OK with the resource returned in the response body
I am seeking opinions/ advice because I am basing my opinion on (a lot) of reading and less so on experience and this seems different from most recommendations to initially return a 202 Accepted and to use 204 only in response to http DELETE
You can choose to return 202 Accepted
upon POST:
The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place.
Now as explained here, you can alter your response body or headers to include something like a Status
value indicating whether the resource generation has completed when the user performs a GET request. Any positive status code will do.
Alternatively, as advocated here, you can also return 202
for the GET while the entity is being created.
All other applicable status codes can be considered 'definitive', because when a client receives 204 No Content
, it cannot differentiate between "entity not yet generated" and "entity was generated, it is empty".
上一篇: 用Golang的网络/ http标头碎片