简体   繁体   中英

HTTP status code 200 vs 202

I have a Python + requests script.

Steps that script should execute :

  • send file to DB;
  • approve this file (change file state in 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 . As I understand (tell me if I wrong) status 202: Accepted means that server accept request and return status code without actual request completion

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 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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