2

If someone could please help settle this argument we might actually get this system finished LOL :^)

So, if you have a REST API.. for.. say.. returning patient details...

And you send in a request with a patient id...
But no patient with that patient id actually exists in the database..

What response should your API return?

 1. a 404  ?  
 2. a 204  ? 
 3. a 200 with something in the body to indicate no patient found..

Thanks

Vida
  • 480
  • 7
  • 18

2 Answers2

2

Use a 404:

404 Not Found

The server can not find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 to hide the existence of a resource from an unauthorized client. This response code is probably the most famous one due to its frequent occurrence on the web.

From MDN Web docs https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

aureliar
  • 1,476
  • 4
  • 16
1

What response should your API return?

It Depends.

Status codes are metadata in the transfer of documents over a network domain. The status code communicates the semantics of the HTTP response to general purpose components. For instance, it's the status code that announces to a cache whether the message body of the response is a "representation of the resource" or instead a representation of an error situation.

Rows in your database are an implementation detail; as far as REST is concerned, there doesn't have to be a database.

What REST cares about is resources, and in this case whether or not the resource has a current representation. REST doesn't tell you what the resource model should be, or how it is implemented. What REST does tell you (via it's standardized messages constraint, which in this case means the HTTP standard) is how to describe what's happening in the resource model.

For example, if my resource is "things to do", and everything is done, then I would normally expect a GET request for "things to do" to return a 2xx status code with a representation announcing there is nothing to do (which could be a completely empty document, or it could be a web page with an empty list of items, or a JSON document.... you get the idea).

If instead the empty result set from the database indicates that there was a spelling error in the URI, then a 404 is appropriate.

It might help to consider a boring web server, and how retrieving an empty file differs from retrieving a file that doesn't exist.

But, as before, in some resource models it might make sense to return a "default" representation in the case where there is no file.


if you have a REST API.. for.. say.. returning patient details...

Is it reasonable in the resource model to have a document that says "we have no records for this patient"?

I'm not a specialist in the domain of medical documents, but it sounds pretty reasonable to me that we might get back a document with no information. "Here's a list of everything we've been told about this patient" and a blank list.


What response should your API return?

If you are returning a representation of an error - ie, a document that explains that the document someone asked for is missing, then you should use a 404 Not Found status code (along with other metadata indicating how long that response can be cached, etc).

If you are returning a document, you should use a 200 OK with a Content-Length header.

204 is specialized, and should not be used here. The key distinction between 204 and 200 with Content-Length 0 is the implications for navigation.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91