bharat nc
bharat nc

Reputation: 127

How to send grpc meta-data from client side

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)?

Upvotes: 2

Views: 7815

Answers (1)

SwiftD
SwiftD

Reputation: 6079

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

Upvotes: 10

Related Questions