I finally found my way: https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md
So basically, grpc.SetHeader()
+ grpc.SendHeader()
and grpc.SetTrailer()
are totally what I was looking for. On the client side, grpc.Header()
and grpc.Trailer()
functions need to be passed to the RPC call, and their argument is a metadata.MD
object to be filled.
On the client side, define your receiving metadata:
var header, trailer metadata.MD
Then, pass it to the SomeRPCCall()
unary RPC:
response, err := client.SomeRPCCall(
context.Background(),
proto.MyMessage{},
grpc.Header(&header),
grpc.Trailer(&trailer),
)
And now, you can check what's in your metadata:
for key, value := range header {
fmt.Printf("%s => %s", key, value)
}
for key, value := range trailer {
fmt.Printf("%s => %s", key, value)
}
On the server side, you can:
-
force the data to be sent right after the RPC is received (but before it's processed):
grpc.SendHeader(ctx, metadata.New(map[string]string{"my-key": "my-value"}))
-
or set & send the metadata at the end of the RPC process (along with the Status
):
grpc.SetTrailer(ctx, metadata.New(map[string]string{"my-key": "my-value"}))