Correct HTTP status code for existent resource, but non-existent entity?

前端 未结 3 1915
一生所求
一生所求 2021-01-01 16:45

Say the client is requesting the following URL:

/user-details?user=123

If /user-details was a non-existing resource, the corre

相关标签:
3条回答
  • 2021-01-01 17:11

    Try 422 which is used in WebDav? See http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

    For me 404 status is ok too (better normed actually), 400 is too vague.

    0 讨论(0)
  • 2021-01-01 17:12

    The 404 is fine because the user-details resource is a conceptual mapping to the user entity in this case to a partial user resource information.

    The GET method for user-details is therefore not responsible for differentiating from the two cases: a) The user doesn't exist, b) The user details don't exist.

    I would however rewrite the endpoint to something like this:

    /user/123/details
    

    Which in my opinion is more expressive.

    0 讨论(0)
  • 2021-01-01 17:21

    The user parameter is part of the resource identifier as stated in RFC 3986, section 3.4:

    The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource within the scope of the URI's scheme and naming authority

    Hence, 404/Not found is perfectly fine.

    0 讨论(0)
提交回复
热议问题