代理如何访问grpc服务中的请求标头。 Golang的grpc-gateway

代理如何访问grpc服务中的请求标头。  Golang的grpc-gateway

问题描述:

I have a grpc server proxied by grpc-gateway. When I make a HTTP call to the gateway endpoint, my corresponding grpc service method is called. Now, the grpc service implementation receives a Context which has the headers. I couldn't figure out how to access the headers.

enter image description here

When I debug my grpc service and put a breakpoint, this is the structure of the Context object which my service receives. Now, how can I get the value of any of the HTTP request headers?

我有一个由grpc-gateway代理的grpc服务器。 当我对网关端点进行HTTP调用时,将调用我相应的grpc服务方法。 现在,grpc服务实现接收到一个具有标头的Context。 我不知道如何访问标题。 p>

“在此处输入图片描述” p>

当我调试我的 grpc服务并放置一个断点,这是我的服务接收到的Context对象的结构。 现在,如何获取任何HTTP请求标头的值? p> div>

HTTP headers are stored in metadata.

md, ok := metadata.FromIncomingContext(ctx) should work for getting the incoming metadata.