Should a RESTful API return 404 for arrays of objects?

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

    Do not return arrays. Return an object in any case, like

    { 
       offset: 30,
       limit: 10,       
       arr: []
    }
    

    it allows you to add metadata (for pagination or smth else)

    usually with http status code: 200

    Also, this is conventional web API behavior, driven by security concerns: https://www.owasp.org/index.php/OWASP_AJAX_Security_Guidelines#Always_return_JSON_with_an_Object_on_the_outside

提交回复
热议问题