我正在制作一个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/