I'm building an ODATA compliant API using ASP.NET WebAPI?
Got a question about how it should behave when a $filter has no results?
Should it return an empty collection? or send HTTP 404 response?
Any ideas/reasoning? I'm biased towards returning an empty collection, but would it violate a recommended practice?
Given you are not directly addressing an individual resource at a specific known Request-Uri (e.g. /resource/{uid}) and your collection is also a known addressable Request-Uri (e.g. /resource) then a 404 would be inappropriate.
An empty collection is what I would expect if I was consuming your API.
I do not think HTTP 404 should be used. The code is used to indicate a reference to a non-existing resource.
The HTTP code 204 (no content) might be a better choice then 404. But an empty collection is a better idea because it would make it easier to use the API.
Stick to the OData specifications 3.0:
9.1.1. 200 OK Response Code
A GET, PUT, MERGE, or PATCH request MAY return 200 OK if the operation is completed successfully. In this case, the response body MUST contain the value of the entity or property specified in the request URL.
9.2.1. 404 Not Found Response Code
If the entity or collection specified by the request URL does not exist, the service SHOULD respond with 404 Not Found and an empty response body.
or even more precise in OData specifications 4.0
9.1.1 Response Code 200 OK
A request that does not create a resource returns 200 OK if it is completed successfully and the value of the resource is not null. In this case, the response body MUST contain the value of the resource specified in the request URL.
9.2.1 Response Code 404 Not Found 404 Not Found indicates that the resource specified by the request URL does not exist. The response body MAY provide additional information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With