How to distinguish 404 between entity doesn't exist and incorrect endpoint?

前端 未结 4 944
無奈伤痛
無奈伤痛 2021-01-02 00:34

Using REST principles, 404 seems to be used to indicate that an entity does not exist. However, how can clients distinguish this case from hitting an incorrect endpoint alt

4条回答
  •  梦毁少年i
    2021-01-02 01:09

    Assuming a framework laid out like this:

    / -- root
    |____+
         /object
         |____+
              /members
              |____+
                   /attributes
                   |____+
                        /attribute_1
                        /attribute_2
                        ...
                        /attribute_n
    

     
    If you mean that you want to be able to distinguish between someone hitting
    /object/members/attributes/incorrect_attribute
    (a 404 using all the right commands, but attempting to retrieve a non-existent resource)
    and someone hitting /object/members/big-bird
    (Assuming that members cannot be a valid endpoint on its own
    [and that /object/members/attributes is not a valid endpoint either])
    then I believe that you could return either a 501 error (not implemented) or a 403 error (Forbidden) depending on where you wanted to place the blame. (Alternately, 418 (I'm a teapot) is also valid here).

    EDIT:
    Finally, if attribute_n used to exist and no longer does, you could respond with a 410 (resource gone).

    See: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

提交回复
热议问题