带有副作用的错误的 REST HTTP 状态代码
REST HTTP status code for error with side effects
我有一个具有大量验证的 HTTP POST 方法。有些意思是请求完全无效,我们 return 一个 400 Bad Request
。
然而,对于其中一些,即使在请求无效的情况下,我们也需要创建一个资源和 return 在正文中的表示。使用什么是正确的状态代码?
200 OK
似乎是错误的,因为请求无效
400 Bad Request
似乎是错误的,因为结果我们创建了一个资源
编辑:请求是 link 系统中的两个对象,在本例中是 member
到 ticket
。但是,即使请求无效,例如因为票已经分配给另一个成员,我们希望在 'ticketLink' table 中存储针对 ticket
的请求记录'ticket-already-assigned' 的状态和 return 响应中的验证消息。
the request is to link two objects in the system, in this case a member to a ticket. However, even if the request is invalid, for example because the ticket is already assigned to another member, we want to store a record of the request against the ticket in a 'ticketLink' table with a status of 'ticket-already-assigned', and return that validation message in the response.
简答:200。
稍微长一点的回答
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.
自从 API 成功地做到这一点后,正确的信号发送方式是 200
内容正文提供了额外的信息。
就像 REST 中的常见情况一样 - 正确使用的模拟是网站的模拟。人类填写表格,然后点击提交按钮申请机票。返回的是一个 HTML 文件说,"we're sorry, that's no longer available, here are some links to other options."
状态代码 200 向 浏览器(和中介)发出文档传输成功的信号。
4xx系列都是文件传输出错;具体来说,客户端发送的请求在某种程度上被破坏了。这里不是这种情况(您正在处理域模型中的错误)。
坦率地说,这甚至不是域模型中的错误,对吧?该模型不应该让您领取已被其他人领取的机票;所以它做了正确的事。
REST API 消费者使用响应中的消息而不是传输状态代码来了解处理的结果是什么,以及现在可用的协议。
我有一个具有大量验证的 HTTP POST 方法。有些意思是请求完全无效,我们 return 一个 400 Bad Request
。
然而,对于其中一些,即使在请求无效的情况下,我们也需要创建一个资源和 return 在正文中的表示。使用什么是正确的状态代码?
200 OK
似乎是错误的,因为请求无效
400 Bad Request
似乎是错误的,因为结果我们创建了一个资源
编辑:请求是 link 系统中的两个对象,在本例中是 member
到 ticket
。但是,即使请求无效,例如因为票已经分配给另一个成员,我们希望在 'ticketLink' table 中存储针对 ticket
的请求记录'ticket-already-assigned' 的状态和 return 响应中的验证消息。
the request is to link two objects in the system, in this case a member to a ticket. However, even if the request is invalid, for example because the ticket is already assigned to another member, we want to store a record of the request against the ticket in a 'ticketLink' table with a status of 'ticket-already-assigned', and return that validation message in the response.
简答:200。
稍微长一点的回答
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.
自从 API 成功地做到这一点后,正确的信号发送方式是 200
内容正文提供了额外的信息。
就像 REST 中的常见情况一样 - 正确使用的模拟是网站的模拟。人类填写表格,然后点击提交按钮申请机票。返回的是一个 HTML 文件说,"we're sorry, that's no longer available, here are some links to other options."
状态代码 200 向 浏览器(和中介)发出文档传输成功的信号。
4xx系列都是文件传输出错;具体来说,客户端发送的请求在某种程度上被破坏了。这里不是这种情况(您正在处理域模型中的错误)。
坦率地说,这甚至不是域模型中的错误,对吧?该模型不应该让您领取已被其他人领取的机票;所以它做了正确的事。
REST API 消费者使用响应中的消息而不是传输状态代码来了解处理的结果是什么,以及现在可用的协议。