go data_error ai_generated true

rpc 错误:code = Internal,desc = 头部键 "X-User-ID" 包含不可打印 ASCII 字符的值

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

ID: go/grpc-metadata-key-invalid

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

版本兼容性

版本状态引入弃用备注
google.golang.org/grpc 1.0+ active

根因分析

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

English

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

解决方案

  1. 95% 成功率
    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% 成功率
    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)
    }

无效尝试

常见但无效的做法:

  1. 70% 失败

    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% 失败

    The metadata is malformed deterministically; retries fail identically.