Should a RESTful API return 400 or 404 when passed an invalid id

后端 未结 6 1911
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 06:02

When building a RESTful API and a user provides an id of resource that does not exist, should you return 404 Not Found or 400 Bad Request

6条回答
  •  悲哀的现实
    2020-12-13 06:39

    According to the RFC (https://tools.ietf.org/html/rfc2616#section-10.4) the API should return 404 when

    "The server has not found anything matching the Request-URI",

    which is your example.

    400 would be when the resource is found, but the request itself is malformed.

    For instance: i. https://api.domain.com/v1/resource/foobar

    where foobar DOES NOT exist should return 404

    ii. https://api.domain.com/v1/resource/foobar where foobar DOES exist, but the request is wrong ({age:"NOTANINTEGER"}, a string instead of an int for example), it should return 400.

    Hope I could help.

提交回复
热议问题