go runtime_error ai_generated true

http: 请求中的body为nil

http: nil body in request

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

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 60% 失败

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

  2. 100% 失败

    Panics occur