POST 的适当 HTTP 状态代码以创建超出限制的资源

Appropriate HTTP Status Code for POST to create resource exceeding the limit

我一直在玩 REST API。我发现了一个错误,我的 POST 请求用于获得 HTTP-401 代码的响应。尽管如此,资源已创建。这不是它应该如何工作的。这让我觉得这个 API 不是世界上最伟大的 API。

然后我修复了我的请求,一切看起来都很好,但是 REST API 返回了 HTTP-400。原因是 "resource limit was exceeded"。

我的问题来了: 处理这种情况的正确 HTTP 状态代码是什么?我的意思是,客户端超出了资源限制,但他仍然发送 POST 请求。 我相信 HTTP-400 是针对格式错误的请求,但所有内容的格式都正确。我们不应该使用其他代码吗?如果有,是哪一个? HTTP-429 Too Many Requests好像也不好,也许HTTP-422 Unprocessable Entity

What is the correct HTTP Status Code to handle the situation?

状态代码,如请求方法,是故意粗粒度的;我们并没有试图非常精确地描述问题,而是广泛地描述问题的一般类别,以便通用客户端(如浏览器和缓存)可以在响应中看到该代码时做正确的事情(对于示例:观察 401 Unauthorized)

时抛出登录对话框

有一些决策树可以帮助select合适的代码;我想我看到 Michael Koprat's most often; these tend not to be comprehensive, but usually cover all of the codes defined by HTTP -- you could also mine the status code registry 其他选项。

Then I fixed my request, everything looked fine but then the REST API returned HTTP-400. The reason was "resource limit was exceeded".

“资源限制”对我来说似乎有点模棱两可,因为预期的意思可能是资源的大小太大——换句话说,message-body/content-length 太大了(413 Payload Too Large would seem appropriate); alternatively, it might be an attempt to communicate that a quota has been exceeded a la 508 Resource Limit is Reached.

4xx 应该表示客户端可以修复的问题 - 请求问题,而不是主机问题。因此,这可能暗示 413 的含义是预期的。

但是如果在请求有问题的情况下,其他状态码的none似乎是匹配的,400 Bad Requestfine.

对正在发生的事情的细粒度解释应该在邮件正文中表示,但这并不总是发生。

Body was NOT too large, the same request executed after deleting one of the resources got a successful response (that's why I think HTTP-400 is incorrect).

迷人:4xx Too Many Notes。我认为您可以为 403 Forbidden and 405 Method Not Allowed 提供一个有效载荷,将这种情况描述为临时情况并提出可能的补救措施。

And is it really a server's error? Not client's one?

在大多数情况下,如果客户端 可以 解决问题,那么您应该使用 4xx class 状态代码的成员。