简体   繁体   English

REST api对GET的预期响应

[英]rest api expected response for GET

I am working on making a rest api and I have a question about what is typically returned from a get with no match. 我正在制作一个REST API,我对一个没有匹配项的get通常返回什么有疑问。 For instance if my resource is "users" and I make an api call with 例如,如果我的资源是“用户”,而我使用

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

If '123' exists I will return a http code of 200 with the details of the user in the response body. 如果存在“ 123”,我将在响应正文中返回一个包含用户详细信息的http代码200。 My question is; 我的问题是; what http code should I return, and what should I put in the response body if there is no user with the id of 123? 如果没有ID为123的用户,我应该返回什么http代码,应该在响应正文中放入什么?

Should I just return a code of 200 and an empty body? 我应该只返回代码200和一个空的正文吗?

Use 404 , because requested resource (User) not exist. 使用404 ,因为请求的资源(用户)不存在。

10.4.5 404 Not Found 10.4.5找不到404

The server has not found anything matching the Request-URI . 服务器未找到与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. 如果服务器通过某种内部可配置的机制得知旧资源永久不可用并且没有转发地址,则应使用410(已消失)状态代码。 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 RFC 2616

As its your API, so you can return whatever you want but if you talked about the standard of HTTP responses than I would suggest you to return 404 (NOT FOUND) in that case which looks more suitable in this case. 作为它的API,您可以返回任何想要的信息,但是如果您谈论HTTP响应的标准,那么我建议您返回404 (NOT FOUND)在这种情况下看起来更合适。 Because the resource you are trying to access is not available at that location. 因为您尝试访问的资源在该位置不可用。

404- Not Found implies the server has not found anything matching the Request-URI. 404-找不到表示服务器未找到与请求URI匹配的任何内容。

With status code 404 you can also provide the response body or any other meaningful header with proper error message. 使用状态码404,您还可以为响应正文或任何其他有意义的标头提供正确的错误消息。 So that client can understand it. 使客户可以理解它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM