可重试的 HTTP 错误应包含哪些信息?
仅说“稍后重试”并不够。客户端需要知道问题是否暂时、何时重试,以及首次请求是否已产生影响。使用状态码和 Retry-After,并配合 API 自定义的 Problem Details 扩展。
发布 更新
仅说“稍后重试”并不够。客户端需要知道问题是否暂时、何时重试,以及首次请求是否已产生影响。使用状态码和 Retry-After,并配合 API 自定义的 Problem Details 扩展。
HTTP/1.1 503 Service Unavailable
Retry-After: 5
Content-Type: application/problem+json
{
"type": "https://api.example.com/problems/temporarily-unavailable",
"title": "Service temporarily unavailable",
"status": 503,
"detail": "The request was not accepted. Retry after the indicated delay.",
"instance": "urn:request:01JEXAMPLE",
"retryable": true,
"operation_result": "not_applied",
"request_id": "01JEXAMPLE"
}Recommended fields and retry safety
503 表示暂时不可用,429 表示请求限流。Retry-After 给出等待秒数或 HTTP 日期。用稳定的 type 或文档化代码分支,不要解析自由文本 detail。示例中的 retryable 和 operation_result 是 API 扩展,不是 RFC 标准字段。
Sources
[RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html)
[RFC 6585](https://www.rfc-editor.org/rfc/rfc6585.html)
[RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html)