Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What http return code should be if no data available

For example i have an api method /api/orders.getOrders which actually always exists. If this method returns no data in following format, should i send 404 or 200 http response code?

{ "orders":[] }

like image 274
Outofdate Avatar asked Jul 29 '16 12:07

Outofdate


1 Answers

200 is correct.

From RFC 7231

The 4xx (Client Error) class of status code indicates that the client seems to have erred.

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource

In your case, the client did not make a mistake in asking for the resource; the origin server did find a current representation of the resource, so 404 (indeed, the entire 4xx class of responses) is not appropriate.

204 is also wrong.

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body.

"No content" means that the HTTP response message body is empty, which is to say the representation being returned is 0 bytes long. It's not appropriate when returning a non empty representation of an empty resource.

like image 124
VoiceOfUnreason Avatar answered Oct 08 '22 16:10

VoiceOfUnreason