go runtime_error ai_generated true

net/http: invalid header field value for "X-Custom-Header"

ID: go/http-header-value-invalid

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-04-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.21 active

Root Cause

Header values contain characters not allowed by HTTP spec (e.g., newlines, control chars), causing WriteHeader to fail.

generic

中文

头部值包含HTTP规范不允许的字符(如换行、控制字符),导致WriteHeader失败。

Workarounds

  1. 90% success
    Use httpguts.ValidHeaderFieldValue to validate before setting
  2. 85% success
    Sanitize input by stripping invalid characters: strings.Map(func(r rune) rune { if r < 32 || r > 126 { return -1 }; return r }, val)

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Escaping may change semantics; proper sanitization needed

  2. 40% fail

    May break client expectations if header is required