go runtime_error ai_generated true

http: nil body in request

ID: go/http-client-do-with-nil-body

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-06-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 95% success
    req, _ := http.NewRequest("GET", url, nil) // nil is fine for GET
  2. 90% success
    Use http.NoBody explicitly: http.NewRequest("POST", url, http.NoBody)

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Still creates a non-nil body that may send Content-Length: 0

  2. 100% fail

    Panics occur