Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing REST ful ODATA APIs, How to act when result set empty? 404 or {}?

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?

like image 222
BuddhiP Avatar asked Jan 11 '13 06:01

BuddhiP


3 Answers

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.

like image 184
Mark Jones Avatar answered Oct 22 '22 17:10

Mark Jones


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.

like image 1
Dmitry S. Avatar answered Oct 22 '22 16:10

Dmitry S.


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.

like image 2
GeoTask Avatar answered Oct 22 '22 16:10

GeoTask