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":[]
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With