xref: /tonic/examples/src/json-codec/client.rs (revision de2e4ac0)
1 //! A HelloWorld example that uses JSON instead of protobuf as the message serialization format.
2 //!
3 //! Generated code is the output of codegen as defined in the `build_json_codec_service` function
4 //! in the `examples/build.rs` file. As defined there, the generated code assumes that a module
5 //! `crate::common` exists which defines `HelloRequest`, `HelloResponse`, and `JsonCodec`.
6 
7 pub mod common;
8 use common::HelloRequest;
9 
10 pub mod hello_world {
11     include!(concat!(env!("OUT_DIR"), "/json.helloworld.Greeter.rs"));
12 }
13 use hello_world::greeter_client::GreeterClient;
14 
15 #[tokio::main]
main() -> Result<(), Box<dyn std::error::Error>>16 async fn main() -> Result<(), Box<dyn std::error::Error>> {
17     let mut client = GreeterClient::connect("http://[::1]:50051").await?;
18 
19     let request = tonic::Request::new(HelloRequest {
20         name: "Tonic".into(),
21     });
22 
23     let response = client.say_hello(request).await?;
24 
25     println!("RESPONSE={:?}", response);
26 
27     Ok(())
28 }
29