7

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?

Kristiono Setyadi
  • 5,635
  • 1
  • 19
  • 29
zdlgrj
  • 99
  • 1
  • 1
  • 9
  • 2
    Possible duplicate of [Proper REST response for empty table?](http://stackoverflow.com/questions/13366730/proper-rest-response-for-empty-table) – jannis Mar 04 '16 at 22:22
  • I'll just leave this here: http://stackoverflow.com/a/20924573/3012385 – DaSourcerer Mar 04 '16 at 22:56
  • 1
    Ideally your REST response status code should be based on the net result of the request. If the request gives a desired response than status code should be 2xx and if the response is other way round then it should always give error response (4xx) . So in your case it should be 404. – Pradeep Sep 14 '16 at 09:28
  • In you case; 404. Simple as that. – gimlichael Mar 10 '17 at 10:11

1 Answers1

2

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.

Paul Draper
  • 78,542
  • 46
  • 206
  • 285