go data_error ai_generated true

rpc error: code = Internal desc = header key "X-User-ID" contains value with non-printable ASCII characters

ID: go/grpc-metadata-key-invalid

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/grpc 1.0+ active

Root Cause

gRPC metadata keys must be lowercase ASCII; values must be printable ASCII. A key with uppercase letters or a value containing control characters violates the spec.

generic

中文

gRPC 元数据键必须为小写 ASCII;值必须为可打印 ASCII。含大写字母的键或含控制字符的值违反规范。

Workarounds

  1. 95% success
    Normalize keys to lowercase and use -bin suffix for binary values:
    md := metadata.Pairs(
        "x-user-id", userID,
        "x-trace-bin", string(traceBytes),
    )
    ctx = metadata.NewOutgoingContext(ctx, md)
  2. 85% success
    Sanitize values before attaching:
    func sanitize(s string) string {
        return strings.Map(func(r rune) rune {
            if r >= 0x20 && r < 0x7f { return r }
            return -1
        }, s)
    }

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Base64 helps only for binary values when the key ends in -bin; a non-printable value on a non-bin key still fails.

  2. 95% fail

    The metadata is malformed deterministically; retries fail identically.