# Get "https://api.example.com/data"：上下文截止时间已超过

- **ID:** `go/http-request-canceled-context`
- **领域:** go
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

http.Client 请求的上下文在服务器响应前超时或被取消。常见原因：context.WithTimeout 设置过短、上游过慢，或在多个请求间复用了已取消的 context。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.18 | active | — | — |
| 1.21 | active | — | — |
| 1.22 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
resp, err := client.Do(req)
   ```
2. **** (80% 成功率)
   ```
   Use context.WithoutCancel(parentCtx) when a downstream call must outlive the parent request lifetime.
   ```

## 无效尝试

- **** — If the deadline comes from the request context, the client Timeout is irrelevant; the context still fires first. (70% 失败率)
- **** — Retrying with the same canceled context immediately fails again and amplifies load. (85% 失败率)
