如果要自定义 http 状态码该从什么区间开始比较合理且不会和预定义状态码冲突?

因为最近在遵循 RESTful 规范写 api 接口,根据规范应当使用 http 状态码表示操作结果,而非在结果集中:

{
    code: 200 , 
    msg: '' ,
    ....
}

返回。碰到一种场景:

某些特殊错误(比如需要根据具体的状态码客户端需要做进一步操作),需要返回明确的状态码,而非和普通错误一样。

自定义的 http 状态码 应该在什么区间定义合理?且不会和预定义状态码 或 保留的状态码 冲突?

阅读 4.4k
1 个回答

参考 RFC https://tools.ietf.org/html/r...

For example, if an unrecognized status code of 471 is received by a
client, the client can assume that there was something wrong with its
request and treat the response as if it had received a 400 (Bad
Request) status code.  The response message will usually contain a
representation that explains the status.

错误代码第一位遵守协议,用 1-5 代表,后两位如果不认识,客户端会假设为 100 / 200 / 300 / 400 / 500

The status codes listed below are defined in this specification,
Section 4 of [RFC7232], Section 4 of [RFC7233], and Section 3 of
[RFC7235].  The reason phrases listed here are only recommendations
-- they can be replaced by local equivalents without affecting the
protocol.

部分通用的建议不要覆盖,但是可以自定义覆盖

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进