api - GET 的其余 api 预期响应

标签 api rest http-response-codes

我正在制作一个rest api,我有一个关于不匹配的 get 通常返回什么的问题。例如,如果我的资源是“users”并且我使用 进行 api 调用

http 方法:GET 网址:api.mysite.com/users/123

如果“123”存在,我将返回一个 http 代码 200,并在响应正文中包含用户的详细信息。我的问题是;我应该返回什么http代码,如果没有id为123的用户,我应该在响应正文中放入什么?

我应该只返回代码 200 和空正文吗?

最佳答案

使用404,因为请求的资源(用户)不存在。

10.4.5 404 Not Found

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable. RFC 2616

关于api - GET 的其余 api 预期响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21210109/

相关文章:

php - 无法为 api 路由 laravel 设置 cookie

java - 使用 Twitter 搜索 API 时出现 400 错误代码

rest - 无法通过 OAuth 使用 Magento REST API

rest - Meteor.js 应用程序和 API

rest - 资源最大响应代码。 REST API 的限制?

java - Android:如何验证我的 Google 帐户以及如何获取我的 GMail 任务?

python - 使用 Django Rest Framework 返回当前用户

python - 将 API 结果导出到 CSV 文件(分隔方式正确吗?)

google-chrome - Chrome 会记住 301 重定向多久?

c# - 是否有用于添加 HttpResponseHeader 的内容 Header Type?