aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/cpp/metadata/README.md
blob: 96ad3d19bdbc9c98533f65ef3799c89bb6f621da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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 to your local machine by running the following command:
 ```sh
$ git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
```
 Change your current directory to examples/cpp/metadata
 ```sh
$ cd examples/cpp/metadata
```

### Generating gRPC code
 To generate the client and server side interfaces:
 ```sh
$ make helloworld.grpc.pb.cc helloworld.pb.cc
```
Which internally invokes the proto-compiler as:
 ```sh
$ 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:

```sh
$ make
```

Run the server, which will listen on port 50051:

```sh
$ ./greeter_server
```

Run the client (in a different terminal):

```sh
$ ./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.