Esun Kim
e7afadc255
|
4 years ago | |
---|---|---|
.. | ||
BUILD | Refactor examples BUILD files | 4 years ago |
Makefile | Revert "Revert "Metadata tutorial"" | 6 years ago |
README.md | get rid of the https://grpc.io/release plague | 5 years ago |
greeter_client.cc | Replaced grpc::string with std::string | 5 years ago |
greeter_server.cc | Revert "Revert "Metadata tutorial"" | 6 years ago |
README.md
Metadata Example
Overview
This example shows you how to add custom headers on the client and server and how to access them.
Custom metadata must follow the "Custom-Metadata" format listed in https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md, with the exception of binary headers, which don't have to be base64 encoded.
Get the tutorial source code
The example code for this and our other examples lives in the examples
directory. Clone this repository
at the latest stable release tag to your local machine by running the following command:
$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
Change your current directory to examples/cpp/metadata
$ cd examples/cpp/metadata
Generating gRPC code
To generate the client and server side interfaces:
$ make helloworld.grpc.pb.cc helloworld.pb.cc
Which internally invokes the proto-compiler as:
$ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto
$ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto
Try it!
Build client and server:
$ make
Run the server, which will listen on port 50051:
$ ./greeter_server
Run the client (in a different terminal):
$ ./greeter_client
If things go smoothly, you will see in the client terminal:
"Client received initial metadata from server: initial metadata value" "Client received trailing metadata from server: trailing metadata value" "Client received message: Hello World"
And in the server terminal:
"Header key: custom-bin , value: 01234567" "Header key: custom-header , value: Custom Value" "Header key: user-agent , value: grpc-c++/1.16.0-dev grpc-c/6.0.0-dev (linux; chttp2; gao)"
We did not add the user-agent metadata as a custom header. This shows how the gRPC framework adds some headers under the hood that may show up in the metadata map.