简体   繁体   English

REST API设计问题

[英]REST API design issue

I am calling a REST API for a list of resources and getting the json response as below if atleast one resource is there. 我正在调用REST API以获得资源列表,并且如果存在至少一个资源,则获取如下所示的json响应。

{
"value": [
    "res1_id",
    "res2_id",
    "res3_id"
]
}

and the HTTP response code is 200. HTTP响应代码为200。

But when no resource is there the server is returning HTTP response code as 404. My doubt it why it is designed that way(it is an enterprise product). 但是,当没有资源时,服务器将返回HTTP响应代码404。我怀疑为什么要这样设计(这是一种企业产品)。 I was expecting a empty list as below and HTTP response code 200: 我期待的是一个空列表,如下所示,HTTP响应代码为200:

{
"value": []
}

I am not able to understand what design decision has been taken into consideration to return 404 instead of an empty json. 我无法理解返回404(而不是空json)时要考虑的设计决策。 Please help me to understand the reasoning behind it. 请帮助我了解其背后的原因。

What I am reading here is that you have a 'collection' resource. 我在这里阅读的是您有一个“收藏”资源。 This collection resource contains a list of sub-resources. 此集合资源包含子资源列表。

There's two possible interpretations for this case. 对于这种情况有两种可能的解释。

  1. The collection resource is 0-length. 收集资源的长度为0。
  2. The collection resource doesn't exist. 集合资源不存在。

Most API's will treat a 0-length collection as a resource that still exists, with a 200 OK . 大多数API会将0长度的集合视为仍然存在的资源,并带有200 OK An empty coffee cup is still a coffee cup. 空咖啡杯仍然是咖啡杯。 The coffee cup itself did not disappear after the last drop is gone. 最后一滴消失后,咖啡杯本身并未消失。

There are cases where it might be desirable for a collection resource to 404. For example, if you want to indicate that a collection never existed or never has been created. 在某些情况下,可能需要将集合资源添加到404。例如,如果您要指示一个集合不存在或从未创建过。

One way to think about this is the difference between an empty directory or a directory not existing. 考虑这种情况的一种方法是空目录或不存在的目录之间的区别。 Both cases can 'tell' the developer something else. 两种情况都可以“告诉”开发人员其他情况。

You're trying to access a resource which does not exist. 您正在尝试访问不存在的资源。 That's the reason for 404. 这就是404的原因。

As a simple google search says 正如一个简单的谷歌搜索说

The HTTP 404, 404 Not Found, and 404 error message is a Hypertext Transfer Protocol (HTTP) standard response code, in computer network communications, to indicate that the client was able to communicate with a given server, but the server could not find what was requested. HTTP 404、404未找到和404错误消息是计算机网络通信中的超文本传输​​协议(HTTP)标准响应代码,用于指示客户端能够与给定的服务器进行通信,但是服务器找不到什么被要求。

For your case 对于你的情况

{
"value": []
}

This can be a status code 200 which means the resource is accessed and data is returned which is an empty array or you can customize it to a 404. But for that you've mentioned best is to return 200. 这可以是状态码200,表示资源已访问且返回的数据为空数组,也可以将其自定义为404。但是,您最好提到的是返回200。

Some companies prefer making their errors known instead of hiding them. 一些公司宁愿让他们的错误知道而不是隐藏它们。 I know where I work we shy away from try catch blocks because we want our applications to blow up during testing so we can fix the problem. 我知道我在哪里工作,所以避免使用catch块,因为我们希望在测试过程中应用程序崩溃,从而可以解决问题。 Not hide it. 不隐藏它。 So it's possible that the dev that created the API wants you to use the status code of the response as a way of telling if the service was successful. 因此,创建该API的开发人员可能希望您使用响应的状态代码作为判断服务是否成功的一种方式。

A status code of 404 is the correct response for when there is no resource to return based on a request you made. 状态代码404是在没有资源根据您的请求返回时的正确响应。 You asked for data and there is no data. 您要求提供数据,但没有数据。 It returns an empty response. 它返回一个空响应。 They're relying on you to base your interpretation of the results on the status code. 他们依靠您来根据状态码来解释结果。

https://en.wikipedia.org/wiki/HTTP_404 https://zh.wikipedia.org/wiki/HTTP_404

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

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