go runtime_error ai_generated true

http: wrote more than the declared Content-Length

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-07-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Clients hang waiting for the remaining bytes.

  2. 90% fail

    Headers already sent; no effect.