Reputation: 99
We are designing a public API, and trying to figure out what is the best practice for GET with following cases:
Path param:
/orders/{orderId}
Found: 200 with a response body.
Not Found: 404.
Query param:
/Products/{productId}/orders?color={color}
Found orders: 200 with response body.
Not found: Should this be a 200 or 204 or even a 404 in this case?
In my opinion, it should be 200 or 204 as the resource is found in this case and the query parameter is only performing the filter effect. But should we return a 200 or 204 in this case?
Upvotes: 7
Views: 14739
Reputation: 11
In your Query Param url (/Products/{productId}/orders?color={color}) it would be helpful for the client to distinguish between the client-supplied productId not existing, versus the productId existing but not having any orders with a color of "color".
If the productId was not found, a 404 should be returned. If the productId was found, but it has no orders, then a 200 with an empty list accurately describes the situation. Don't use 204 because 204 is used to indicate that there is literally an empty response, and you are returning an empty list [].
Upvotes: 1
Reputation: 83205
Assuming that
(1) The first URL is for exactly one order.
(2) The second URL is for a list of 0 or more orders.
Missing an order in the first response should be a 404, since a not-an-order is not an order.
Missing orders in the second response should be a 200, since an empty list is still a list.
Upvotes: 3