对于空表适当休息回应? [英] Proper REST response for empty table?

查看:176
本文介绍了对于空表适当休息回应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设你想通过调用来获得用户列表 GET API /用户,但目前该表是截断所以有没有用户。什么是这种情况下适当的反应 404 204


解决方案

我说,没有。

为什么不404(未找到)?

404状态code应预留的情况下,在资源未找到。在这种情况下,你的资源是的用户的集合的。这个集合存在,但它是空的。就个人而言,我会作为客户端的为您的应用笔者很困惑,如果我有一个 200 一天,一个 404 第二天只是因为有人碰巧删除几个用户。我应该做些什么?我的网址错了吗?是不是有人改变API和忽视留下一个重定向。

为什么不204(无内容)?

下面是从摘录的204状态code与W3C <说明/ p>


  

服务器已经完成了请求,但并不需要返回一个实体主体,并且可能希望返回更新的元信息。


虽然这可能在这种情况下似乎是合理的,我认为它会混淆用户。 A 204 应该表明,一些操作执行成功,没有数据需要返回。这是完美的,因为到的响应删除请求或者烧制一些脚本,并不需要返回的数据。在 API /用户的情况下,你通常希望收到你的用户集合的重新presentation。发送一个响应体一次,而不是将其发送其他的时间是不一致的和潜在的误导性。

为什么我会使用一个200(OK)

有关上述第(一致性)提到的原因,我将返回一个空集的重新presentation。让我们假设你正在使用XML。一个正常的身体反应,为用户的非空集可能看起来像这样:

 &LT;&用户GT;
  &lt;使用者&GT;
    &LT; ID&GT; 1 LT; / ID&GT;
    &LT;名称&gt;汤姆和LT; /名称&gt;
  &LT; /使用者名称&gt;
  &lt;使用者&GT;
    &LT; ID&GT; 2'; / ID&GT;
    &LT;名称&gt;&IMB LT; /名称&gt;
  &LT; /使用者名称&gt;
&LT; /用户&GT;

如果该列表是空的,你可以只用像这样回应(同时仍使用 200

 &LT;用户/&GT;

无论采用哪种方式,客户端接收遵循一定的,公知的格式的响应体。有没有不必要的混乱和状态code检查。此外,没有状态code定义侵犯。每个人都高兴。

您可以做同样的JSON或HTML或任何格式的你使用。

Let's say you want to get list of users by calling get to api/users but currently the table was truncated so there are no users. What is the proper response for this scenario 404 or 204?

解决方案

I say, neither.

Why not 404 (Not Found) ?

The 404 status code should be reserved for situations, in which a resource is not found. In this case, your resource is a collection of users. This collection exists but it's currently empty. Personally, I'd be very confused as an author of a client for your application if I got a 200 one day and a 404 the next day just because someone happened to remove a couple of users. What am I supposed to do? Is my URL wrong? Did someone change the API and neglect to leave a redirection.

Why not 204 (No Content) ?

Here's an excerpt from the description of the 204 status code by w3c

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation.

While this may seem reasonable in this case, I think it would also confuse clients. A 204 is supposed to indicate that some operation was executed successfully and no data needs to be returned. This is perfect as a response to a DELETE request or perhaps firing some script that does not need to return data. In case of api/users, you usually expect to receive a representation of your collection of users. Sending a response body one time and not sending it the other time is inconsistent and potentially misleading.

Why I'd use a 200 (OK)

For reasons mentioned above (consistency), I would return a representation of an empty collection. Let's assume you're using XML. A normal response body for a non-empty collection of users could look like this:

<users>
  <user>
    <id>1</id>
    <name>Tom</name>
  </user>
  <user>
    <id>2</id>
    <name>IMB</name>
  </user>
</users>

and if the list is empty, you could just respond with something like this (while still using a 200):

<users/>

Either way, a client receives a response body that follows a certain, well-known format. There's no unnecessary confusion and status code checking. Also, no status code definition is violated. Everybody's happy.

You can do the same with JSON or HTML or whatever format you're using.

这篇关于对于空表适当休息回应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆