You have to insert your metadata into the client's context before calling the server.
For an unary RPC the client side looks like:
conn, _ := grpc.Dial(address, opts...)
client := NewMyClient(conn) // generated from your proto with the grpc protoc option
header := metadata.New(map[string]string{"authorization": "", "space": "", "org": "", "limit": "", "offset": ""})
// this is the critical step that includes your headers
ctx := metadata.NewContext(context.Background(), header)
request := // construct a request for your service
response, err := client.MyMethod(ctx, request)
For a stream, it looks almost the same:
conn, _ := grpc.Dial(address, opts...)
client := NewMyClient(conn) // generated from your proto with the grpc protoc option
header := metadata.New(map[string]string{"authorization": "", "space": "", "org": "", "limit": "", "offset": ""})
// this is the critical step that includes your headers
ctx := metadata.NewContext(context.Background(), header)
stream, err := client.MyMethodStream(ctx)
for {
request := // construct a request for your service
err := stream.Send(request)
response := new(Response)
err = stream.RecvMsg(response)
}
On the server side for an unary RPC:
func (s myServer) MyMethod(context.Context, *Request) (*Response, error) {
headers, ok := metadata.FromContext(ctx)
token := headers["authorization"]
}
and for a streaming RPC:
func (s myServer) MyMethodStream(stream MyMethod_MyServiceStreamServer) error {
headers, ok := metadata.FromContext(stream.Context())
token := headers["authorization"]
for {
request := new(Request)
err := stream.RecvMsg(request)
// do work
err := stream.SendMsg(response)
}
}
Note that for a stream there are only three times that headers can be sent: in the context used to open the initial stream, via grpc.SendHeader, and grpc.SetTrailer. It is not possible to set headers on arbitrary messages in a stream. For an unary RPC header are sent with every message and can be set in the initial context, with grpc.SendHeader and grpc.SetHeader, and grpc.SetTrailer.