xref: /tonic/examples/src/authentication/client.rs (revision 3a5c66d5)
1 pub mod pb {
2     tonic::include_proto!("grpc.examples.echo");
3 }
4 
5 use http::header::HeaderValue;
6 use pb::{echo_client::EchoClient, EchoRequest};
7 use tonic::transport::Channel;
8 
9 #[tokio::main]
10 async fn main() -> Result<(), Box<dyn std::error::Error>> {
11     let channel = Channel::from_static("http://[::1]:50051")
12         .intercept_headers(|headers| {
13             headers.insert(
14                 "authorization",
15                 HeaderValue::from_static("Bearer some-secret-token"),
16             );
17         })
18         .connect()
19         .await?;
20 
21     let mut client = EchoClient::new(channel);
22 
23     let request = tonic::Request::new(EchoRequest {
24         message: "hello".into(),
25     });
26 
27     let response = client.unary_echo(request).await?;
28 
29     println!("RESPONSE={:?}", response);
30 
31     Ok(())
32 }
33