访问损坏/无效资源的REST响应代码
对于已损坏或语义无效的资源,响应HTTP GET时使用的最佳HTTP状态代码是什么访问损坏/无效资源的REST响应代码,rest,http,http-status-codes,Rest,Http,Http Status Codes,对于已损坏或语义无效的资源,响应HTTP GET时使用的最佳HTTP状态代码是什么 例如,考虑对 GET/HOM/1234 的请求,其中在服务器上存在人ID 1234的数据,但违反某些业务规则,因此服务器拒绝使用它。 404不适用(因为数据实际上存在) 一般来说,4xx似乎并不理想(因为问题发生在服务器端,而不是在客户端的控制下) 503似乎适用于整个服务,而不是特定的资源 500当然合适,但实际上告诉客户可能有什么问题是非常模糊的 有什么建议吗?根据: 我认为没有一个4xx状态代码可以作为
例如,考虑对<代码> GET/HOM/1234 的请求,其中在服务器上存在人ID 1234的数据,但违反某些业务规则,因此服务器拒绝使用它。
- 404不适用(因为数据实际上存在)
- 一般来说,4xx似乎并不理想(因为问题发生在服务器端,而不是在客户端的控制下)
- 503似乎适用于整个服务,而不是特定的资源
- 500当然合适,但实际上告诉客户可能有什么问题是非常模糊的
- 案例2已更新,以包括一个可能适用于此类案例的示例。但我又不是专家,我可能是 错了
500 (Internal Server Error)
status code indicates that the server
encountered an unexpected condition that prevented it from fulfilling
the request.
在阅读了评论和链接资源后,似乎@RemyLebeau的方法是最好的: 我认为500是唯一适合这种情况的官方响应代码。没有什么能阻止你加入一个描述失败原因的响应主体
在国际海事组织,500美元最合适。但是,如果您查看规范,您将不会发现任何与您描述的损坏/无效资源明显相关的内容。5xx是服务器端错误,但没有区分到底出了什么问题…有趣的问题…请澄清违反了一些业务规则。我认为500是唯一适合这种情况的官方响应代码。没有什么能阻止您包括一个描述失败原因的响应主体。这里有一个选择适当HTTP状态代码的有用方法。如果数据可以为将来的请求更正,我可以看看503如何用于此。但我认为510不合适,因为没有使用自定义扩展来访问资源。我会坚持使用500,并用一个响应体来描述失败。我从中了解到的是,服务器需要对请求本身进行进一步扩展才能完成它。例如,服务器错误响应可能包括该损坏数据的较旧工作版本的列表,并期望您发送回相同的请求,精确到您需要的较旧版本,以便可以替换或仅获取损坏的数据。你觉得雷米尔博怎么样?这种情况不符合510状态代码的上下文吗?
503 (Service Unavailable)
status code indicates that the server
is currently unable to handle the request due to a temporary overload
or scheduled maintenance, which will likely be alleviated after some
delay. The server MAY send a Retry-After header field
(Section 7.1.3) to suggest an appropriate amount of time for the
client to wait before retrying the request.
510 Not Extended
The policy for accessing the resource has not been met in the
request. The server should send back all the information necessary
for the client to issue an extended request. It is outside the scope
of this specification to specify how the extensions inform the
client.
If the 510 response contains information about extensions that were
not present in the initial request then the client MAY repeat the
request if it has reason to believe it can fulfill the extension
policy by modifying the request according to the information provided
in the 510 response. Otherwise the client MAY present any entity
included in the 510 response to the user, since that entity may
include relevant diagnostic information.
500 (Internal Server Error)
status code indicates that the server
encountered an unexpected condition that prevented it from fulfilling
the request.