1 use h2::client;
2 use http::{HeaderMap, Request};
3
4 use std::error::Error;
5
6 use tokio::net::TcpStream;
7
8 #[tokio::main]
main() -> Result<(), Box<dyn Error>>9 pub async fn main() -> Result<(), Box<dyn Error>> {
10 let _ = env_logger::try_init();
11
12 let tcp = TcpStream::connect("127.0.0.1:5928").await?;
13 let (mut client, h2) = client::handshake(tcp).await?;
14
15 println!("sending request");
16
17 let request = Request::builder()
18 .uri("https://http2.akamai.com/")
19 .body(())
20 .unwrap();
21
22 let mut trailers = HeaderMap::new();
23 trailers.insert("zomg", "hello".parse().unwrap());
24
25 let (response, mut stream) = client.send_request(request, false).unwrap();
26
27 // send trailers
28 stream.send_trailers(trailers).unwrap();
29
30 // Spawn a task to run the conn...
31 tokio::spawn(async move {
32 if let Err(e) = h2.await {
33 println!("GOT ERR={:?}", e);
34 }
35 });
36
37 let response = response.await?;
38 println!("GOT RESPONSE: {:?}", response);
39
40 // Get the body
41 let mut body = response.into_body();
42
43 while let Some(chunk) = body.data().await {
44 println!("GOT CHUNK = {:?}", chunk?);
45 }
46
47 if let Some(trailers) = body.trailers().await? {
48 println!("GOT TRAILERS: {:?}", trailers);
49 }
50
51 Ok(())
52 }
53