0

Let us say we are building an API which returns transactions of a given bank account. If there are no transactions on account to return do we return http 200 with no results found as a response or return http 404 transactions not found?

In another situation, we built an API which returns average salary of the year. If instead of 12 months salary, only 5 months salary is given as input because of some valid reason(may be a new employee), we want to return - Not enough data to calculate yearly average salary. Do we use http 200 send this repose or should we use some other http code?

Its a design question

1 Answer 1

2

Generally speaking, you'd want to align your response codes with what users are expecting.

The 200 response is generally used as an OK response.

As per mdn web docs:

The request succeeded. The result meaning of "success" depends on the HTTP method.

  • GET: The resource has been fetched and transmitted in the message body.
  • HEAD: The representation headers are included in the response without any message body.
  • PUT or POST: The resource describing the result of the action is transmitted in the message body.
  • TRACE: The message body contains the request message as received by the server.

This information is based on RFC 7231

In your situation, using a 404 Not Found response could lead the developer to think the resource does not exist.

A 200 OK is an appropriate response as the HTTP call was successful and you can provide a "no content found" message.

A 204 No Content would work as well but the request should be terminated after the header, thus you're not passing a message.

For your second scenario, since the request is erroneous, you should go for a 4XX or 5XX of your choice. A 400 Bad Request is a possibility as:

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).

Edit: A 2XX response can be considered if the "Not enough data to calculate yearly average salary" message is displayed client side, however since you'd require more data to complete the calculation, a 4XX or 5XX would be more correct.

Sign up to request clarification or add additional context in comments.

2 Comments

For Second Scenario, it is not a bad request if we give only 5 months salary as input. Instead of checking at caller side whether 12 months salary is available, the API checks if it has enough data as input to calculate yearly average salary and if there is not enough data, it says not possible to calculate. Its valid functional scenario. Do we still got with anything other than 2XX?
A 2XX response could be used if that "Not enough data to calculate yearly average salary" message is a client side message indeed. I've editted my original answer. A 4XX or 5XX is still more correct as the data expected to perform the primary purpose is incomplete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.