Should a RESTful API return 404 for arrays of objects?

前端 未结 4 1753
旧巷少年郎
旧巷少年郎 2021-02-03 18:58

Lets say there\'s a Product with Orders. If you ask for /products/product_id, it will return a 404 if product_id doesn\'t exist. But should /products/product_id/orders return a

4条回答
  •  [愿得一人]
    2021-02-03 19:44

    In my opinion:

    We're talking http status values here, and should be of a higher level of giving responses.

    One should see this in layers of delegates. Like when your api is not able to answer a request, in case the api call itself is not available, then you could reply with a 404.

    But when your call exists, and it could reply with a collection of data, but its an empty collection, you could return just a http 200, with an empty result.

    I would use http status values to give an indication on the request validation, and not directly make it dependent on the content in the deeper api layers.

    Or one could strictly follow protocols found on the net, but nobody follows them...

提交回复
热议问题