Home > database >  HTTP status code for resource that is not available yet
HTTP status code for resource that is not available yet

Time:11-03

I have a DB table with a report_url column. As soon as a backend done with filling and storing a report it fills that column with S3 link. If the report was not yet stored, the column value is NULL by default. I also have Pyramid API where an endpoint is declared returning Response with body of report content. So, whenever the user makes request, according controller will be fired to get the report link and download the file and return it to user. However, if report is not done yet (report_url is NULL), I need to inform the user somehow. In this case front-end should receive HTTP status 400, but I have not figured out if this fits best. Or maybe 503 fits better here?

CodePudding user response:

Have a look at available http status codes.

What you probably want is 404, specifically because of this line:

In an API, this can also mean that the endpoint is valid but the resource itself does not exist.:

Full description:

404 Not Found

The server cannot find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client. This response code is probably the most well known due to its frequent occurrence on the web.

If the server is working on getting the report, 102 gets an honorable mention:

102 Processing (WebDAV)

This code indicates that the server has received and is processing the request, but no response is available yet.

it's not part of the standard, it's an extension, WebDAV.

400 status codes are used to let the user know something they did is not working. 500 status codes are used when something is going on with the server. That's how I understand it anyway.

In that way, if this is a "normal" execution of the API/program, perhaps a 200 status code would do just fine. E.g. just define the endpoint to return {"report_url": null} if it isn't ready, otherwise {"report_url": "an actual url"} and then give 200 in each case. And the receiving party handles it depending on if it is null or not. The pro of this method is, now the user can know that it is definitely a proper endpoint (and not an url typo, which would also give 404). However, you could make your own 404 page saying "report is not ready" or "report does not exist" for example. The con of this 200 method is some speed penalty since you have to send an unnecessary response body.

Disclaimer: I am not a web/http expert at all.

CodePudding user response:

The correct HTTP status code is 202 - Accepted. The documentation says:

The 202 (Accepted) status code indicates that the request has been accepted for processing, but the processing has not been completed.

..

The representation sent with this response ought to describe the request's current status and point to (or embed) a status monitor that can provide the user with an estimate of when the request will be fulfilled.

  • Related