如何将元数据从 grpc-gateway 发送到 grpc 服务器?
在客户端(grpc-gateway)上:
func (c *Client) MiddlewareAuth(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
...
ctxOut := metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{
"key": "value",
}))
r = r.WithContext(ctxOut)
h.ServeHTTP(w, r)
})
}
在服务器上:
func (s *Server) List(ctx context.Context, request *pb.Request) (*pb.Response, error) {
md, _ := metadata.FromIncomingContext(ctx)
fmt.Println(md)
return &pb.Response{
Ok: true
}, nil
}