go network_error ai_generated true

http: invalid Content-Length

ID: go/http-multiple-content-length

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.22 active

Root Cause

The HTTP request or response contains multiple Content-Length headers with different values, causing Go's net/http to reject it as invalid.

generic

中文

HTTP请求或响应包含多个值不同的Content-Length头,导致Go的net/http拒绝该请求。

Workarounds

  1. 75% success
    if req.Header.Get("Content-Length") != "" {
        // Validate and use a single value
    } else {
        // Handle missing Content-Length
    }
  2. 80% success
    Use a custom RoundTripper to sanitize headers before sending.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    The body may be corrupted or incomplete, leading to data integrity issues.

  2. 60% fail

    Does not resolve the underlying inconsistency and may cause parsing errors.