传递无效ID时,RESTful API是否应返回400或404 [英] Should a RESTful API return 400 or 404 when passed an invalid id
问题描述
在构建RESTful API时,如果用户提供了不存在的 id 资源,则应返回404 Not Found
或400 Bad Request
.
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
.
例如:
https://api.domain.com/v1/resource/foobar
foobar 不存在的地方.
推荐答案
在资源不存在的情况下,我将返回404(这意味着url路径错误),并且只有在用某些调用进行其余调用的情况下,我才会返回400无效数据(@PathParam),例如
https://api.domain.com/v1/profile/test@email :我在这里尝试获取电子邮件ID的个人资料,但电子邮件本身是错误的,因此我将返回400.
https://api.domain.com/v1/profile1111/test@email.com 将因为网址路径无效,所以返回404.
I would return 404 in case of resource does not exist(means the url path is wrong) and i will return 400 only if the rest call is made with some invalid data (@PathParam) for example
https://api.domain.com/v1/profile/test@email : here i am trying to get profile of email id, but the email itself is wrong, so I will return 400.
https://api.domain.com/v1/profile1111/test@email.com will return 404 because the url path is invalid.
这篇关于传递无效ID时,RESTful API是否应返回400或404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!