| 1 |
# gRPC command line tool |
| 2 |
|
| 3 |
## Overview |
| 4 |
|
| 5 |
This document describes the command line tool that comes with gRPC repository. It is desirable to have command line |
| 6 |
tools written in other languages roughly follow the same syntax and flags. |
| 7 |
|
| 8 |
At this point, the tool needs to be built from source, and it should be moved out to grpc-tools repository as a stand |
| 9 |
alone application once it is mature enough. |
| 10 |
|
| 11 |
## Core functionality |
| 12 |
|
| 13 |
The command line tool can do the following things: |
| 14 |
|
| 15 |
- Send unary rpc. |
| 16 |
- Attach metadata and display received metadata. |
| 17 |
- Handle common authentication to server. |
| 18 |
- Infer request/response types from server reflection result. |
| 19 |
- Find the request/response types from a given proto file. |
| 20 |
- Read proto request in text form. |
| 21 |
- Read request in wire form (for protobuf messages, this means serialized binary form). |
| 22 |
- Display proto response in text form. |
| 23 |
- Write response in wire form to a file. |
| 24 |
|
| 25 |
The command line tool should support the following things: |
| 26 |
|
| 27 |
- List server services and methods through server reflection. |
| 28 |
- Fine-grained auth control (such as, use this oauth token to talk to the server). |
| 29 |
- Send streaming rpc. |
| 30 |
|
| 31 |
## Code location |
| 32 |
|
| 33 |
To use the tool, you need to get the grpc repository and make sure your system |
| 34 |
has the prerequisites for building grpc from source, given in the [installation |
| 35 |
instructions](../BUILDING.md). |
| 36 |
|
| 37 |
In order to build the grpc command line tool from a fresh clone of the grpc |
| 38 |
repository, you need to run the following command to update submodules: |
| 39 |
|
| 40 |
``` |
| 41 |
git submodule update --init |
| 42 |
``` |
| 43 |
|
| 44 |
Once the prerequisites are satisfied, you can build with cmake: |
| 45 |
|
| 46 |
``` |
| 47 |
$ mkdir -p cmake/build |
| 48 |
$ cd cmake/build |
| 49 |
$ cmake -DgRPC_BUILD_TESTS=ON ../.. |
| 50 |
$ make grpc_cli |
| 51 |
``` |
| 52 |
|
| 53 |
The main file can be found at |
| 54 |
https://github.com/grpc/grpc/blob/master/test/cpp/util/grpc_cli.cc |
| 55 |
|
| 56 |
## Prerequisites |
| 57 |
|
| 58 |
Most `grpc_cli` commands need the server to support server reflection. See |
| 59 |
guides for |
| 60 |
[Java](https://github.com/grpc/grpc-java/blob/master/documentation/server-reflection-tutorial.md#enable-server-reflection) |
| 61 |
, [C++](https://github.com/grpc/grpc/blob/master/doc/server_reflection_tutorial.md) |
| 62 |
and [Go](https://github.com/grpc/grpc-go/blob/master/Documentation/server-reflection-tutorial.md) |
| 63 |
|
| 64 |
Local proto files can be used as an alternative. See instructions [below](#Call-a-remote-method). |
| 65 |
|
| 66 |
## Usage |
| 67 |
|
| 68 |
### List services |
| 69 |
|
| 70 |
`grpc_cli ls` command lists services and methods exposed at a given port |
| 71 |
|
| 72 |
- List all the services exposed at a given port |
| 73 |
|
| 74 |
```sh |
| 75 |
$ grpc_cli ls localhost:50051 |
| 76 |
``` |
| 77 |
|
| 78 |
output: |
| 79 |
|
| 80 |
```none |
| 81 |
helloworld.Greeter |
| 82 |
grpc.reflection.v1alpha.ServerReflection |
| 83 |
``` |
| 84 |
|
| 85 |
The `localhost:50051` part indicates the server you are connecting to. |
| 86 |
|
| 87 |
- List one service with details |
| 88 |
|
| 89 |
`grpc_cli ls` command inspects a service given its full name (in the format |
| 90 |
of \<package\>.\<service\>). It can print information with a long listing |
| 91 |
format when `-l` flag is set. This flag can be used to get more details |
| 92 |
about a service. |
| 93 |
|
| 94 |
```sh |
| 95 |
$ grpc_cli ls localhost:50051 helloworld.Greeter -l |
| 96 |
``` |
| 97 |
|
| 98 |
`helloworld.Greeter` is full name of the service. |
| 99 |
|
| 100 |
output: |
| 101 |
|
| 102 |
```proto |
| 103 |
filename: helloworld.proto |
| 104 |
package: helloworld; |
| 105 |
service Greeter { |
| 106 |
rpc SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply) {} |
| 107 |
} |
| 108 |
|
| 109 |
``` |
| 110 |
|
| 111 |
### List methods |
| 112 |
|
| 113 |
- List one method with details |
| 114 |
|
| 115 |
`grpc_cli ls` command also inspects a method given its full name (in the |
| 116 |
format of \<package\>.\<service\>.\<method\>). |
| 117 |
|
| 118 |
```sh |
| 119 |
$ grpc_cli ls localhost:50051 helloworld.Greeter.SayHello -l |
| 120 |
``` |
| 121 |
|
| 122 |
`helloworld.Greeter.SayHello` is full name of the method. |
| 123 |
|
| 124 |
output: |
| 125 |
|
| 126 |
```proto |
| 127 |
rpc SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply) {} |
| 128 |
``` |
| 129 |
|
| 130 |
### Inspect message types |
| 131 |
|
| 132 |
We can use `grpc_cli type` command to inspect request/response types given the |
| 133 |
full name of the type (in the format of \<package\>.\<type\>). |
| 134 |
|
| 135 |
- Get information about the request type |
| 136 |
|
| 137 |
```sh |
| 138 |
$ grpc_cli type localhost:50051 helloworld.HelloRequest |
| 139 |
``` |
| 140 |
|
| 141 |
`helloworld.HelloRequest` is the full name of the request type. |
| 142 |
|
| 143 |
output: |
| 144 |
|
| 145 |
```proto |
| 146 |
message HelloRequest { |
| 147 |
optional string name = 1; |
| 148 |
} |
| 149 |
``` |
| 150 |
|
| 151 |
### Call a remote method |
| 152 |
|
| 153 |
We can send RPCs to a server and get responses using `grpc_cli call` command. |
| 154 |
|
| 155 |
- Call a unary method Send a rpc to a helloworld server at `localhost:50051`: |
| 156 |
|
| 157 |
```sh |
| 158 |
$ grpc_cli call localhost:50051 SayHello "name: 'gRPC CLI'" |
| 159 |
``` |
| 160 |
|
| 161 |
output: `sh message: "Hello gRPC CLI"` |
| 162 |
|
| 163 |
`SayHello` is (part of) the gRPC method string. Then `"name: 'world'"` is |
| 164 |
the text format of the request proto message. For information on more flags, |
| 165 |
look at the comments of `grpc_cli.cc`. |
| 166 |
|
| 167 |
- Use local proto files |
| 168 |
|
| 169 |
If the server does not have the server reflection service, you will need to |
| 170 |
provide local proto files containing the service definition. The tool will |
| 171 |
try to find request/response types from them. |
| 172 |
|
| 173 |
```sh |
| 174 |
$ grpc_cli call localhost:50051 SayHello "name: 'world'" \ |
| 175 |
--protofiles=examples/protos/helloworld.proto |
| 176 |
``` |
| 177 |
|
| 178 |
If the proto file is not under the current directory, you can use |
| 179 |
`--proto_path` to specify new search roots |
| 180 |
(separated by colon on Mac/Linux/Cygwin or semicolon on Windows). |
| 181 |
|
| 182 |
Note that the tool will always attempt to use the reflection service first, |
| 183 |
falling back to local proto files if the service is not found. Use |
| 184 |
`--noremotedb` to avoid attempting to use the reflection service. |
| 185 |
|
| 186 |
- Send non-proto rpc |
| 187 |
|
| 188 |
For using gRPC with protocols other than protobuf, you will need the exact |
| 189 |
method name string and a file containing the raw bytes to be sent on the |
| 190 |
wire. |
| 191 |
|
| 192 |
```bash |
| 193 |
$ grpc_cli call localhost:50051 /helloworld.Greeter/SayHello \ |
| 194 |
--input_binary_file=input.bin \ |
| 195 |
--output_binary_file=output.bin |
| 196 |
``` |
| 197 |
|
| 198 |
On success, you will need to read or decode the response from the |
| 199 |
`output.bin` file. |