Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 1 | # gRPC command line tool |
| 2 | |
| 3 | ## Overview |
| 4 | |
Eric Gribkoff | 132fd42 | 2017-01-26 09:23:33 -0800 | [diff] [blame] | 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. |
Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 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 | |
Yang Gao | 0aa822b | 2016-06-09 17:23:38 -0700 | [diff] [blame] | 11 | ## Core functionality |
Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 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. |
Yuchen Zeng | 9cb9445 | 2016-07-20 16:39:31 -0700 | [diff] [blame] | 18 | - Infer request/response types from server reflection result. |
Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 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. |
Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 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 | |
Eric Gribkoff | 132fd42 | 2017-01-26 09:23:33 -0800 | [diff] [blame] | 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](https://github.com/grpc/grpc/blob/master/INSTALL.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 | You also need to have the gflags library installed on your system. On Linux |
| 45 | systems, gflags can be installed with the following command: |
| 46 | |
| 47 | ``` |
| 48 | sudo apt-get install libgflags-dev |
| 49 | ``` |
| 50 | |
| 51 | Once the prerequisites are satisfied, you can build the command line tool with |
| 52 | the command: |
Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 53 | |
| 54 | ``` |
Yang Gao | 0aa822b | 2016-06-09 17:23:38 -0700 | [diff] [blame] | 55 | $ make grpc_cli |
Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 56 | ``` |
| 57 | |
| 58 | The main file can be found at |
| 59 | https://github.com/grpc/grpc/blob/master/test/cpp/util/grpc_cli.cc |
| 60 | |
| 61 | ## Usage |
| 62 | |
| 63 | ### Basic usage |
| 64 | |
| 65 | Send a rpc to a helloworld server at `localhost:50051`: |
| 66 | |
| 67 | ``` |
Yuchen Zeng | 9cb9445 | 2016-07-20 16:39:31 -0700 | [diff] [blame] | 68 | $ bins/opt/grpc_cli call localhost:50051 SayHello "name: 'world'" \ |
| 69 | --enable_ssl=false |
Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 70 | ``` |
| 71 | |
| 72 | On success, the tool will print out |
| 73 | |
| 74 | ``` |
| 75 | Rpc succeeded with OK status |
Yuchen Zeng | 9cb9445 | 2016-07-20 16:39:31 -0700 | [diff] [blame] | 76 | Response: |
Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 77 | message: "Hello world" |
| 78 | ``` |
| 79 | |
| 80 | The `localhost:50051` part indicates the server you are connecting to. `SayHello` is (part of) the |
Yuchen Zeng | 9cb9445 | 2016-07-20 16:39:31 -0700 | [diff] [blame] | 81 | gRPC method string. Then `"name: 'world'"` is the text format of the request proto message. We are |
| 82 | not using ssl here by `--enable_ssl=false`. For information on more flags, look at the comments of `grpc_cli.cc`. |
| 83 | |
| 84 | ### Use local proto files |
| 85 | |
| 86 | If the server does not have the server reflection service, you will need to provide local proto |
| 87 | files containing the service definition. The tool will try to find request/response types from |
| 88 | them. |
| 89 | |
| 90 | ``` |
| 91 | $ bins/opt/grpc_cli call localhost:50051 SayHello "name: 'world'" \ |
| 92 | --protofiles=examples/protos/helloworld.proto --enable_ssl=false |
| 93 | ``` |
| 94 | |
| 95 | If the proto files is not under current directory, you can use `--proto_path` to specify a new |
| 96 | search root. |
Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 97 | |
| 98 | ### Send non-proto rpc |
| 99 | |
| 100 | For using gRPC with protocols other than probobuf, you will need the exact method name string |
| 101 | and a file containing the raw bytes to be sent on the wire |
| 102 | |
| 103 | ``` |
Yang Gao | 0aa822b | 2016-06-09 17:23:38 -0700 | [diff] [blame] | 104 | $ bins/opt/grpc_cli call localhost:50051 /helloworld.Greeter/SayHello --input_binary_file=input.bin \ |
Yang Gao | ac4b3a6 | 2016-06-08 15:00:25 -0700 | [diff] [blame] | 105 | --output_binary_file=output.bin |
| 106 | ``` |
| 107 | On success, you will need to read or decode the response from the `output.bin` file. |