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