go runtime_error ai_generated true

http:写入的内容超过声明的 Content-Length

http: wrote more than the declared Content-Length

ID: go/http-header-write-after-write

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-07-22首次发现

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.21 active — — —

根因分析

处理程序显式设置了 Content-Length 但写入的字节数超出声明,或中间件重写了 body。

English

Handler set Content-Length header explicitly but wrote more bytes than declared, or middleware re-wrote the body.

generic

解决方案

  1. 90% 成功率
    Buffer the response with bytes.Buffer, then set Content-Length to buf.Len() before w.Write.
  2. 92% 成功率
    Do not set Content-Length manually; let net/http chunk or compute it.

无效尝试

常见但无效的做法:

  1. 85% 失败

    Clients hang waiting for the remaining bytes.

  2. 90% 失败

    Headers already sent; no effect.