go
runtime_error
ai_generated
true
http: nil body in request
ID: go/http-client-do-with-nil-body
80%Fix Rate
87%Confidence
0Evidence
2024-06-30First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
Root Cause
Passing a nil body to http.NewRequest or http.Client.Do, which is not allowed; use http.NoBody instead.
generic中文
向http.NewRequest或http.Client.Do传递nil body,不允许;应使用http.NoBody。
Workarounds
-
95% success
req, _ := http.NewRequest("GET", url, nil) // nil is fine for GET -
90% success
Use http.NoBody explicitly: http.NewRequest("POST", url, http.NoBody)
Dead Ends
Common approaches that don't work:
-
60% fail
Still creates a non-nil body that may send Content-Length: 0
-
100% fail
Panics occur