Sharad Ahire
Sharad Ahire

Reputation: 788

Which http code should return when there is no data

I have written Rest API which return 200 http code when there is data in backend system.however which http status code i should return when there no data in backend system so that it will help client code to interpret response without any ambiguity.

Upvotes: 2

Views: 5872

Answers (2)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57229

I have written Rest API which return 200 http code when there is data in backend system.however which http status code i should return when there no data in backend system so that it will help client code to interpret response without any ambiguity.

Quick review; the status-code describes...

the result of the server's attempt to understand and satisfy the client's corresponding request. The rest of the response message is to be interpreted in light of the semantics defined for that status code.

In other words, it's a shorthand classification of the response, not the state of the backend system.

which http status code i should return when there no data in backend system so that it will help client code to interpret response without any ambiguity.

It depends on what "no data in backend system" means for your context.

If no data in the backend system indicates an error by the client, then you should be returning a status-code from the Client Error 4xx class.

If no data in the backend system indicates an error by the server, then you should be returning a status-code from the Server Error 5xx class.

If there have been no errors in the system at all (the request is good, the server is handling the request correctly), then you should be returning a status-code from the Successful 2xx class

204 No Content has a precise meaning; it advises the client (and intermediary components) that the origin server's response includes no content in the response payload body.

A 204 response is terminated by the first empty line after the header fields because it cannot contain a message body.

In other words, if your representation of no data available is an empty application/json object

{}

or an empty application/json array

[]

or an application/json null

null

and you are sending that representation to the client, then using the 204 status-code is not compliant with RFC 7321. 200 is almost certain to be the correct code to use in these cases.

Upvotes: 5

Zooby
Zooby

Reputation: 323

HTTP code 204 is used to say the request was successful and there's nothing to return. If there's no data found and that's believed to be an error, there's the classic 404. HTTP status codes

Upvotes: 3

Related Questions