区分来自网络的相同状态代码中的消息 api
Differentiate messages within same status code from web api
我正在开发一个网站 API,出于多种原因,它将 return Forbidden
http 状态-
- 用户被阻止。
- IP 被封锁。
- 超出请求限制。
- 用户未验证。
我return正在按以下方式回复-
return Content(HttpStatusCode.Forbidden, message); //message : "Limit reached" etc.
现在,我需要在客户那里以不同的方式处理这些原因。我应该从 returned 消息中找出原因吗?此消息将显示给用户,将来可能会更改。
最佳做法是什么?
您的错误消息应该针对您的客户进行描述。理想情况下,您需要包含以下信息:
- 开发人员消息(技术说明 - “
cache is out-of-date
”、“service A is unavailable
”)
- human-readable 消息(业务描述 - “
this time is booked already
”、“company A is closed and cannot be modified
”)
- 提示如何解决("
refresh your cache
", "try again in 5 minutes
", "request permission A from administrator
")
- href 到您的联机文档
- Header 喜欢“
error_code: 100
”。文档中还应描述自定义代码。您可以在客户的代码中依赖并适当处理这个
还有你说的是什么意思:
User is unverified.
如果用户未通过身份验证(api 不知道 他是谁),api 应该 return 401
http状态码。如果用户缺少某些权限,因此无法修改资源,那么 403
完全没问题。但是,最好能准确描述缺少哪个权限
What is the best practice for this?
与其尝试发明自己的模式,不如寻找标准化的东西,例如 Problem Details for HTTP APIs。
问题详细信息描述了一个人类可读的“标题”和“详细信息”字段,以及一个 link 人类可读文档的“类型”。
Consumers MUST use the "type" string as the primary identifier for the problem type; the "title" string is advisory and included only for users who are not aware of the semantics of the URI and do not have the ability to discover them (e.g., offline log analysis). Consumers SHOULD NOT automatically dereference the type URI.
Consumers SHOULD NOT parse the "detail" member for information; extensions are more suitable and less error-prone ways to obtain such information.
我正在开发一个网站 API,出于多种原因,它将 return Forbidden
http 状态-
- 用户被阻止。
- IP 被封锁。
- 超出请求限制。
- 用户未验证。
我return正在按以下方式回复-
return Content(HttpStatusCode.Forbidden, message); //message : "Limit reached" etc.
现在,我需要在客户那里以不同的方式处理这些原因。我应该从 returned 消息中找出原因吗?此消息将显示给用户,将来可能会更改。
最佳做法是什么?
您的错误消息应该针对您的客户进行描述。理想情况下,您需要包含以下信息:
- 开发人员消息(技术说明 - “
cache is out-of-date
”、“service A is unavailable
”) - human-readable 消息(业务描述 - “
this time is booked already
”、“company A is closed and cannot be modified
”) - 提示如何解决("
refresh your cache
", "try again in 5 minutes
", "request permission A from administrator
") - href 到您的联机文档
- Header 喜欢“
error_code: 100
”。文档中还应描述自定义代码。您可以在客户的代码中依赖并适当处理这个
还有你说的是什么意思:
User is unverified.
如果用户未通过身份验证(api 不知道 他是谁),api 应该 return 401
http状态码。如果用户缺少某些权限,因此无法修改资源,那么 403
完全没问题。但是,最好能准确描述缺少哪个权限
What is the best practice for this?
与其尝试发明自己的模式,不如寻找标准化的东西,例如 Problem Details for HTTP APIs。
问题详细信息描述了一个人类可读的“标题”和“详细信息”字段,以及一个 link 人类可读文档的“类型”。
Consumers MUST use the "type" string as the primary identifier for the problem type; the "title" string is advisory and included only for users who are not aware of the semantics of the URI and do not have the ability to discover them (e.g., offline log analysis). Consumers SHOULD NOT automatically dereference the type URI.
Consumers SHOULD NOT parse the "detail" member for information; extensions are more suitable and less error-prone ways to obtain such information.