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
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.
解决方案
-
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) -
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) }
无效尝试
常见但无效的做法:
-
70% 失败
Base64 helps only for binary values when the key ends in -bin; a non-printable value on a non-bin key still fails.
-
95% 失败
The metadata is malformed deterministically; retries fail identically.