MCPcopy Create free account
hub / github.com/bytecodealliance/wstd / main

Function main

examples/http_client.rs:41–112  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

39
40#[wstd::main]
41async fn main() -> Result<()> {
42 let args = Args::parse();
43
44 // Create and configure the `Client`
45
46 let mut client = Client::new();
47
48 if let Some(connect_timeout) = args.connect_timeout {
49 client.set_connect_timeout(*connect_timeout);
50 }
51 if let Some(first_byte_timeout) = args.first_byte_timeout {
52 client.set_first_byte_timeout(*first_byte_timeout);
53 }
54 if let Some(between_bytes_timeout) = args.between_bytes_timeout {
55 client.set_between_bytes_timeout(*between_bytes_timeout);
56 }
57
58 // Create and configure the request.
59
60 let mut request = Request::builder();
61
62 request = request.uri(args.url).method(args.method);
63
64 for header in args.headers {
65 let mut parts = header.splitn(2, ": ");
66 let key = parts.next().unwrap();
67 let value = parts
68 .next()
69 .ok_or_else(|| anyhow!("headers must be formatted like \"key: value\""))?;
70 request = request.header(key, value);
71 }
72
73 // Send the request.
74
75 let body = if args.body {
76 Body::from_try_stream(wstd::io::stdin().into_inner().into_stream())
77 } else {
78 Body::empty()
79 };
80
81 let request = request.body(body)?;
82
83 eprintln!("> {} / {:?}", request.method(), request.version());
84 for (key, value) in request.headers().iter() {
85 let value = String::from_utf8_lossy(value.as_bytes());
86 eprintln!("> {key}: {value}");
87 }
88
89 let response = client.send(request).await?;
90
91 // Print the response.
92 eprintln!("< {:?} {}", response.version(), response.status());
93 for (key, value) in response.headers().iter() {
94 let value = String::from_utf8_lossy(value.as_bytes());
95 eprintln!("< {key}: {value}");
96 }
97
98 let body = response.into_body().into_boxed_body().collect().await?;

Callers

nothing calls this directly

Calls 12

stdinFunction · 0.85
emptyFunction · 0.85
stdoutFunction · 0.85
set_connect_timeoutMethod · 0.80
into_streamMethod · 0.80
into_boxed_bodyMethod · 0.80
nextMethod · 0.45
into_innerMethod · 0.45
sendMethod · 0.45
write_allMethod · 0.45

Tested by

no test coverage detected