Akash Kumar
Akash Kumar

Reputation: 642

In REST API call what should we return in case of empty result

I am having 2 services-
1. Client Service
2. Feature Service

Client Service asks feature service for feature corresponding to a client. If in the database no feature found for a client should what should we return?
1. 404 no resource found
2. 204 Success - No content
3. 200 Success - {features:[]} // Empty list

Upvotes: 0

Views: 1670

Answers (3)

Sean
Sean

Reputation: 106

In practice, I have always returned a 200 with an empty list. The fact that the list is empty tells the consumer that, although nothing went wrong with the request, there are no resources associated with it.

The 204 is a fair compromise between the 200 and the 404 but I think that it adds minimal value.

I would personally shy away from throwing a 404 on an endpoint that returns a list since it is expected that there will be situations where the list is empty (that is potentially too broad of a statement). If the request was failing because of the resource associated with the list then I would definitely consider the 404 valid (let's say you had a user with a list of hats and the request for the hats was for a NOT FOUND user).

Upvotes: 1

Anuj Vishwakarma
Anuj Vishwakarma

Reputation: 842

You can return in two ways. 404 is not valid if no content is present.

a) 204 No Content
Explaination: The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation.


b) Return 200 and with empty body {};

You can find more at w3 https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Upvotes: 0

Jay seen
Jay seen

Reputation: 511

HTTP response should be 200, Just show "empty" result in TAG

Upvotes: 0

Related Questions