HTTP 状态代码 200 与 202

HTTP status code 200 vs 202

我有一个 Python+requests 脚本。

脚本应该执行的步骤:

约束条件

只能下载批准的文件

我的代码:

requests.post(url_to_create, files={"file": open(path_to_file)})
requests.post(url_to_approve, data={'id': file_id})
requests.get(url_to_download, data={'id': file_id})

问题

这段代码几乎可以完美运行,但有时我找不到文件。我发现第一个和第三个请求 return 200 状态码,而第二个 returns 202。据我了解(如果我错了请告诉我)状态 202: Accepted 表示服务器接受请求和 return 状态代码,但没有实际请求完成

问题

这是否意味着即使请求批准尚未完成也可以发送下载请求,如果是这样,我如何等到批准请求完成后再发送下载请求?

这取决于您的服务器实现,您的服务器决定如何处理 202

202 Accepted

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. There is no facility for re-sending a status code from an asynchronous operation such as this.

The 202 response is intentionally non-committal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed. The entity returned with this response SHOULD include an indication of the request's current status and either a pointer to a status monitor or some estimate of when the user can expect the request to be fulfilled.

如果响应 body 为空,检查响应 headers 应该有附加信息是有意义的。

参考 - https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html