0

What is the best practice in return codes if a call to a rest service executes a database query, no results are found and it returns.

Should this be a 404 or a 200 with a message stating no records?

The URL has resolved correctly and no server error has occurred just that there are no records returned.

EDIT: Example url is:

http://localhost/app/pr_xyz/1234

Used to retrieve a list of xyz that belong to the existing user 1234. The user id 1234 has already been retrieved and is known to exist in the database. This URL (pr_xyz) is just to retrieve a list of xyz that belong to that user.

The relationship is 1 user to 0 or many xyz.

In this case the existing user has no xyz. Should this be a 404 or 200 with meaningful message.

Also I have no control over the URL.

Gurnard
  • 1,773
  • 22
  • 43
  • 4
    200 in my opinion , with a meaningful message – Satya Jun 18 '15 at 11:34
  • Noooo. Not 200. If a resource can't be found, it should be a 404. See [Is it correct to return 404 when a REST resource is not found?](http://stackoverflow.com/q/26845631/2587435). – Paul Samsotha Jun 18 '15 at 12:37
  • 2
    @peeskillet in this instance the resource can be found and it executes correctly. The rest service resolves correctly just the result of the query is no records. The link you post talks about retrieving a service and what to do when it cannot find the service, for sure that should be a 404. – Gurnard Jun 18 '15 at 12:38
  • What do you define as the "resource"? Do you mean the JAX-RS _"method"_? The method itself is not the resource. The resource is what should be returned from a particular URI. Say a URI is `../users/123`. The resource is the user, and it is identified by that URI. If the user can't be found, it should be a 404. This is general use case where the `123` might be part of URI template. The JAX-RS method handles all requests to `@Path("users/{id}")`. This method handle attempts at finding different resources, depending on the id. If it can't be found == 404 – Paul Samsotha Jun 18 '15 at 12:44
  • The link has nothing to do with finding a service. I think your main confusion is the fact that you thing the _"method"_ or JAX-RS rest service endpoint is the "resource", which this is not the case. – Paul Samsotha Jun 18 '15 at 12:47

1 Answers1

3

Agree with @Satya, it should be a 200, but we can arrive at the answer by working backwards.

So, we start with the full list of HTTP status codes.. Start from the bottom.

  • Is it a server error? No? Then it's not a 5xx
  • Is it a client error? Maybe? Perhaps it's not a 4xx
  • It's obviously not a redirect, so it's not a 3xx.
  • Is it a successful call? Perhaps.
  • Are you returning a provisional response? No, it's not a 1xx either.

So by process of elimination, which are looking at the 2xx codes. Exactly which would be a good fit depends on the semantics of your application.

Since, this is not a DELETE call, then I probably wouldn't use 204 No Content, which is probably the only good alternative. It's not that there's no content.

There is content: "0 results found". Google doesn't show you a blank page when there are no search results.

So we arrive at 200, as well as returning a meaningful body. If you were returning raw results, then you might want to consider wrapping them in a search-results object to provide some meta-data.

Ah! So wait, what if instead of search results, you are indicating a collection of RESTful resources?

GET /items

In this case, I would still return a 200 with a body that says there are no items.

What if, you were trying to retrieve a particular resource?

GET /items/1234

In this case, yes, you want to imply that item 1234 does not exist, and you should return a 404.

Chris Scott
  • 1,721
  • 14
  • 27
  • So are you saying that if a method (endpoint) had a URI template of `"/users/{id}"`, and requested URI is `"/users/123"`, and the database is queried from that `{id}` and nothing is found, you would still return 200? – Paul Samsotha Jun 18 '15 at 12:52
  • 1
    No, the last paragraph of my answer specifically says to return a 404 in that case. The original question implied many results (i.e. not a single resource) were possible so 200 becomes an alternative. – Chris Scott Jun 18 '15 at 13:01
  • Unless the OP is asking specifically about accessing a collection resource that returns an empty result set, I would say this doesn't answer the main concern. I may have misunderstood the question, but I did not read it this way. – Paul Samsotha Jun 18 '15 at 13:42