go
runtime_error
ai_generated
true
http: 请求中的body为nil
http: nil body in request
ID: go/http-client-do-with-nil-body
80%修复率
87%置信度
0证据数
2024-06-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
根因分析
向http.NewRequest或http.Client.Do传递nil body,不允许;应使用http.NoBody。
English
Passing a nil body to http.NewRequest or http.Client.Do, which is not allowed; use http.NoBody instead.
解决方案
-
95% 成功率
req, _ := http.NewRequest("GET", url, nil) // nil is fine for GET -
90% 成功率
Use http.NoBody explicitly: http.NewRequest("POST", url, http.NoBody)
无效尝试
常见但无效的做法:
-
60% 失败
Still creates a non-nil body that may send Content-Length: 0
-
100% 失败
Panics occur