xref: /tonic/examples/src/grpc-web/client.rs (revision b29f4668)
1 use hello_world::{greeter_client::GreeterClient, HelloRequest};
2 use tonic_web::GrpcWebClientLayer;
3 
4 pub mod hello_world {
5     tonic::include_proto!("helloworld");
6 }
7 
8 #[tokio::main]
9 async fn main() -> Result<(), Box<dyn std::error::Error>> {
10     // Must use hyper directly...
11     let client = hyper::Client::builder().build_http();
12 
13     let svc = tower::ServiceBuilder::new()
14         .layer(GrpcWebClientLayer::new())
15         .service(client);
16 
17     let mut client = GreeterClient::with_origin(svc, "http://127.0.0.1:3000".try_into()?);
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