dongyao2129 2018-03-12 20:24
浏览 350
已采纳

如何从客户端发送grpc元数据

I'm using grpc.Dial(server) in golang to setup a grpc connection to my server.

How do I send meta-data or custom headers through this Dial connection (from client side)?

  • 写回答

1条回答 默认 最新

  • dp518158 2018-03-12 20:47
    关注

    I presume grpc is your client connection object.

    You create meta data using the metadata subpackage from a map[string]string (some other methods provided), you then pass it via context e.g

    md := metadata.New(map[string]string{"key1": "val1", "key2": "val2"})
    ctx := metadata.NewOutgoingContext(context.Background(), md)
    

    Because you pass meta data via context you will need to use client.DialContext() rather than Dial I think https://godoc.org/google.golang.org/grpc#DialContext

    See here for some examples - https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突