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

后端 未结 6 1906
伪装坚强ぢ
伪装坚强ぢ 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:40

    Should be 404 ( Not Found ). 400 is used if you can't fulfill the request due to bad syntax, however for your case, the syntax is correct, however there is no resource foobar.

    You can use 400 if user uses non-existent API like below :

    https://api.domain.com/v1/nonexistAPI/xyz/xyz
    

    You can also refer to this REST API Design Blog which tell you how to design your REST error codes.

提交回复
热议问题