What should I return in a REST API when the results are either true or false?

0

When I search an information in the database and this information is not returned (since it does not exist), what should I do? Filling a JSON object for GET return? And if I return false?

Can the response be the status code 201 or 404? What would be the best practice?

    
asked by anonymous 21.03.2018 / 20:14

1 answer

0

The APIs I'm currently using return NULL in case of false and 1 in case of true in one sub array. Returning 404 is not ideal as it refers to "address not found" and implies that the guy has requested for a wrong url. I recommend returning code 200 even and always return an array with query information (number of records, pages, etc) and a sub array in the end containing the data.

So, if you make a request to your API and it checks that there is no record to be returned, the user will receive code 200 and you can see that you requested it, but no records were found in the database.

    
21.03.2018 / 20:27