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

Asked

Viewed 322 times

-1

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

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

  • I edited it to make it clearer what you wanted. It’s not return true or false but return a response or not. In your case is to return or not a respsota.

  • 1

    It was not I who denied, in fact I found the good question

  • And that’s no reason to be negative, too !

  • Quiet. The question got better! Thank you!

1 answer

0

The API I currently use returns NULL in the event of false and 1 in the event of true in a sub array. Returning 404 is not ideal, as it refers to "address not found" and hints that the guy requested for a wrong url. I recommend to resume code 200 and always return an array with query information (number of records, pages, etc) and a sub array inside containing the data.

So, if they make a request to your API and it checks that there is no record to be returned, the user will receive code 200 and can see that ordered right, but no records were located in the BD.

  • 1

    I learned (these times actually) the HTTP 204 status, which is a "Success" that returns "No Content" ("No Content"), which would be a kind of "failure" (the request was successful, processed, and returned nothing). Those who taught me said that a lot of people make mistakes in this, thinking that HTTP 2xx codes include only "successful results".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.