Should a RESTful API return 404 for arrays of objects?

前端 未结 4 1743
旧巷少年郎
旧巷少年郎 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:32

    You really should do only one of two things

    Either Return a 200 (OK) status code, and an empty array in the body.

    Or Return a 204 (NO CONTENT) status code and NO response body.

    To me, option 2 seems more technically correct and keeping in line with REST and HTTP principles.

    However, option 1 seems more efficient for the client - because the client does not need extra logic to differentiate between two (success) status codes. Since it knows that it will always receive an array, it simply has to check for whether it got none, one, or many items and process it appropriately

提交回复
热议问题