Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 252
Andersson Avatar asked Jul 14 '16 12:07

Andersson


People also ask

What is difference between 200 and 201 status code?

Perhaps the most common status code returned is 200. It simply means that the request was received, understood, and is being processed, whereas the 201 status code indicates that a request was successful and a resource was created as a result.

What is a 202 status code?

The HyperText Transfer Protocol (HTTP) 202 Accepted response status code indicates that the request has been accepted for processing, but the processing has not been completed; in fact, processing may not have started yet.

What does a status code 200 mean?

The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.

Should Put return 200 or 204?

200 OK - This is the most appropriate code for most use-cases. 204 No Content - A proper code for updates that don't return data to the client, for example when just saving a currently edited document. 202 Accepted - If the update is done asynchronous, this code can be used.


1 Answers

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

like image 179
gevorg Avatar answered Sep 25 '22 12:09

gevorg