无法插入资源但可以由客户端修复时的 HTTP 状态引用

HTTP status quote when a resource can't be inserted but could be fixed by the client

我有一个资源要通过 POST 插入;说预订。在某些情况下,服务器无法插入给定的对象,但需要向用户反馈,需要更改什么才能插入它。例如,如果您发送包含以下内容的预订:

{
  "start": "2020-09-14T15:00:00Z",
  "end": "2020-09-14T15:00:00Z",
  "memberId": 5,
  "seat": "A5",
}

如果服务器发现座位 A5 已被占用,但想要 return 一个在该时间范围内空闲的座位列表,我会使用什么?

409 Conflict 好像有点像我想要的,因为在它的规范中你可以告诉用户哪里出了问题。但是我不确定这是否算作冲突。

409 Conflict seems to be somewhat like what I want, because in its specification you can tell the user what's wrong.

所有 4xx 响应代码都允许您告诉用户出了什么问题 - 请参阅 RFC 7231

the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.

也就是说,409 很好

要考虑的另一个合理替代方案是 403 Forbidden

The 403 (Forbidden) status code indicates that the server understood the request but refuses to authorize it.

这两个代码的规范文本不同,但如果您查看规范,并没有太多迹象表明通用组件对它们的处理方式有所不同 - 它们都不可缓存,也不会对语义进行任何重大更改响应 headers,依此类推。

鉴于效果相同,您主要是在“您想在访问日志中看到什么?”、“我们想发出哪个通用警报”等问题之间进行选择。

由于您的操作员可能不希望这些信号与损坏的 http 请求 (400) 和安全渗透尝试 (403) 交叉,因此为与您的内部域逻辑冲突的请求选择不同的代码将使他们的工作更轻松.