0

So, I have an RESTful API on a blog site (Wordpress, but that's irrelevant). I understand that if, for example, there are 0 posts in the category news, the route /posts/by-category/news/?limit=100&offset=0 should return a 200 OK response with a body like {total: 0, items: []}, which is quite logical. But what if the category news does not exist? Should the route still return a 200 OK response with {total: 0, items: []} body, or shall it return a 404 Not Found response?

  • Depends how you running the application. Based on category you filter data or you have separate table where you get data. To filter you can give 200 as query will not return any data. But if you fetching category from db then it will not exists so it should 404. – Dhaval Gajjar Apr 12 '23 at 08:42
  • if category does not exists, maybe 400 BadRequest should be the answer. If exists and returns 0 posts in category, 200 OK with that body is correct. 404 Nof Found maybe is better for an specific resource, in this case, /posts/by-category/news/{id} and is not found – Talenel Apr 12 '23 at 08:45
  • Well, there is also `/posts/` route that shows all the posts without category filter, so, due to namespace, I guess, `/posts/by-category//` can be considered filter. Although, it is different route – LeTraceurSnork Apr 12 '23 at 08:45

1 Answers1

0

Extending on the answer given by Dhaval.

As per RFC specs response codes 2xx and 4xx have different meanings.

Good read about that: HTTP Get with 204 No Content: Is that normal

To conclude it depends on how you run your web app.

  1. Route one could be your return 200 code. and then show a message basis on the length of the result set returned in API call
  2. You return 204 code which equates to 'No Content' in RFC specifications.

Hope this works. Thanks.

Suchandra T
  • 569
  • 4
  • 8