I am trying to code an API which has a long running process to which an end user may make a POST request:

POST /things { "some":"json" }

The actual creation process can take some time, will often be queued. It might take many minutes. As a result I am not sure what I should be returning or when. Is it the usual 201 plus object, returned after whatever time it takes my API to create the object? Isn't this going to cause problems at the client end? Is there some other standard way to do this - such as an intermediate step?

I'm using Rails & Grape for my API if that helps.

有帮助吗?

解决方案

Consider whether the Post-Redirect-Get pattern suits your needs. For example, you can return a 303 redirect to some sort of status page where the client can check the progress of the request. In general, 201+object is a poor choice if the client has to wait for any appreciable period, because too many things can go wrong (what if out of annoyance or impatience he kills the browser window, or refreshes, or resubmits?)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top