可重試的 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)