api-guidelines icon indicating copy to clipboard operation
api-guidelines copied to clipboard

NotFound question

Open sublimemm opened this issue 6 years ago • 17 comments

Whats your position on returning a 200 with an empty body/collection when there's a collection endpoint and the results are empty?

People tend to think this is obviously a 404 situation, but this leaves the client unable to differentiate between the wrong endpoint and the correct endpoint but no results.

Do you have the same opinion when it's an entity endpoint vs a collection endpoint? e.g., /myThing/123 is not found. /mything/123 is a valid endpoint, it's just that entity 123 is gone.

sublimemm avatar Sep 01 '20 21:09 sublimemm

Agreed. As someone who has done some deep Apache and IIS module work, 404's were intended to indicate a URI was invalid. That's very different than responding to a query that has a zero result set.

doug-ross avatar Sep 03 '20 16:09 doug-ross

Is there an appropriate 2xx response code for an empty response body that indicates no-content? 204 is close, but semantically relates to a PUT/POST on create-update-delete https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204

Larkenx avatar Sep 03 '20 16:09 Larkenx

Actually the purpose of 404 response code is to notify the initiator that the resource addressed in the URL is not found. If the URL was not valid I think that is another response code. I think 400 Bad Request would be the best for a wrong URL.

cwoodruff avatar Sep 03 '20 19:09 cwoodruff

@cwoodruff - agreed. Probably didn't state that clearly enough. If the URI specifies a resource that was not found, it should generate a 404.

doug-ross avatar Sep 03 '20 19:09 doug-ross

We would always say that should return a 200 with an empty collection. The call succeeded, the collection exists. There's just nothing in it.

Given we always follow the convention of having the array itself be a 'value' property on a top-level object representing the collection itself, there's also the ability to return collection metadata that is valid regardless of whether there is anything in the collection, e.g. MaxItems.

gareth-a-jones avatar Sep 03 '20 20:09 gareth-a-jones

@garethj-msft I was mistaken and I agree with you with a caveat. I have always architected my APIs to return a 204 when there is an empty response body. Now if you are returning an empty set and have an empty json enclosure within your response body then yes a 200 would be correct but if your API only returns a single resource and there is no content in the response body then a 204 would be the better response code to signal the API initiator that the body is empty and no attempt should be taken to examine it.

cwoodruff avatar Sep 03 '20 20:09 cwoodruff

@garethj-msft yep, that's our thinking on the collection endpoints as well.

What about on a regular entity endpoint e.g., /mything/123 where it is a valid url, but mything 123 doesn't exist?

sublimemm avatar Sep 03 '20 20:09 sublimemm

That's a tougher question, because the spec is somewhat ambiguous (i.e., "incorrect parameters" should generate a 404). Does that mean parameter names or parameter values? The spec also states the "HTTP status codes should be used".

My opinion only: if the app URI works and the endpoint has to look into a data-store to determine 123 doesn't exist, that's still an application-level error resulting in an empty set. The intermingling of server responses with application responses just strikes me as a bit confusing and perhaps contributing to different interpretations of the spec.

doug-ross avatar Sep 03 '20 21:09 doug-ross

Consider that API retrieves "resources" (or objects, as you wish) as a webSite retrieves pages. If your request targets an user by its name and the user with requested name does not exists, you can consider that the "requested resource" does not exists (or not found), right? Now, consider that api results Ok code. With an empty content, where is the requested data?

I agree that this question is tougher. But I prefer returns the "Not Found" in both cases: endpoint incorrect and the user not found.

ThiagoDaher avatar Sep 03 '20 21:09 ThiagoDaher

Thiago, that's a damn good point. I hadn't thought about arguments as part of a "virtual URI".

This one is really tough and well above my pay-grade!

doug-ross avatar Sep 03 '20 21:09 doug-ross

Incorrect parameters should results BadRequest, in my opinion, Doug.

Believe me, your "pay-grade" becomes very higher than mine... Here in Brazil, things are really bad... And it is not a company issue. It is on whole country, and since long time ago... :-(

ThiagoDaher avatar Sep 03 '20 21:09 ThiagoDaher

This is exactly the crux the of the question. Is the NotFound for the URI or the resource?

https://tools.ietf.org/html/rfc7231#section-6.5.4

You can read this an thing either way... "the endpoint /mything/123 was called and I successfully fulfilled the request, the response is 204 No Content at that URI"

or "the endpoint /mything/123 was called and I successfully fulfilled the request, the response is 404 Not Found"

To me, it seems against the spirit of 4xx codes to return a 4xx when you DID, in fact, process the request successfully. Successfully can mean No Content.

sublimemm avatar Sep 04 '20 13:09 sublimemm

I believe that NotFound is related to both, sublimemm. NoContent represents a task that does not produces a result, but something has been done in server side. No content has been returned due the fact that there is no result to be returned AND this is the expected scenario. All parts of your request has been informed and are all correct (server, path, filter and parameters).

In other hand, NotFound represents that the requested object cannot be found, but this IS NOT the expected scenario. The reasons does not matter here, but can be exposed in content as developers can format an user friendly message or more detailed information that points why your request cannot be processed as expected.

Results that are simply empty does not tells to the client what is wrong. But NotFound gives to user at least some idea about what is incorreto. If it can be corrected, changing the parameter value to an existing resource, the response becomes as expected.

ThiagoDaher avatar Sep 04 '20 16:09 ThiagoDaher

I can see the argument either way, but they key thing really in the spirit of guidelines is that for customers' sake, you are internally consistent, consistent across your products and where possible consistent with industry norms.

Our choice is that this individual scenario always returns a 404.

Does anyone believe there's a clarification to the guidelines necessary from this discussion and if so is willing to submit a PR?

gareth-a-jones avatar Sep 04 '20 18:09 gareth-a-jones

@sublimemm What I do not like about 404 is that it may be caused by many different things like wrong reverse proxy configuration, wrong routing in the service, etc.

The more I work with external APIs the more I like those which always return 2xx (or 5xx in case of internal failure), but the payload contains information about operation result: whether it is a success or failure.

In other words, I don't like when HTTP Status Codes are used for describing business related things.

voroninp avatar Sep 06 '20 08:09 voroninp

@voroninp yep, those are exactly some of the reasons our internal teams have complained about this 404 scenario.

@garethj-msft I do think the section collections should call out that it never returns a 404. I can start a PR. I'd prefer you leave the individual entity situation ambiguous so that we don't have to tell people we conflict with the MS guidelines :)

sublimemm avatar Sep 08 '20 20:09 sublimemm

This has been a great discussion folks and lead to an improvement in the guidelines. I'm going to close it up now.

gareth-a-jones avatar Feb 07 '21 04:02 gareth-a-jones