简体   繁体   English

HTTP状态代码200与202

[英]HTTP status code 200 vs 202

I have a Python + requests script. 我有一个Python + requests脚本。

Steps that script should execute : 脚本应该执行的步骤

  • send file to DB; 发送文件到DB;
  • approve this file (change file state in DB); 批准此文件(更改DB中的文件状态);
  • download file. 下载文件。

The constraint : 约束

Only approved file could be downloaded 只能下载已批准的文件

My code : 我的代码

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})

The problem : 问题

This code works almost perfectly, but sometimes I get no file. 这段代码几乎完美,但有时我没有文件。 I found that the first and the third requests return 200 status code while the second returns 202 . 我发现第一个和第三个请求返回200状态代码,而第二个请求返回202 As I understand (tell me if I wrong) status 202: Accepted means that server accept request and return status code without actual request completion 据我所知(告诉我,如果我错了)状态202: Accepted意味着服务器接受请求并返回状态代码而没有实际请求完成

The question : 问题

Does it mean that request to download could be send even if request to approve hasn't been already completed and, if it is so, how can I wait till approval-request completed before send download-request? 这是否意味着即使请求批准尚未完成也可以发送下载请求,如果是,那么在发送下载请求之前我怎么能等到批准请求完成?

It depends on your server implementation and your server decides how 202 will be processed. 这取决于您的服务器实现,您的服务器决定如何处理202

202 Accepted 202接受

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. 202回复是故意不承诺的。 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. 返回此响应的实体应该包括请求的当前状态的指示,以及指向状态监视器的指针或用户可以期望满足请求的某些估计。

If response body is empty, makes sense to check response headers that should have additional information. 如果响应主体为空,则检查应具有其他信息的响应标头是有意义的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM