Examples: allow passing URL via CLI

This commit is contained in:
Sean McArthur
2022-01-07 11:03:46 -08:00
parent a03ca5012d
commit 7388b676df
2 changed files with 36 additions and 8 deletions

View File

@@ -6,13 +6,29 @@
#[cfg(not(target_arch = "wasm32"))]
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let res = reqwest::get("https://hyper.rs").await?;
// Some simple CLI args requirements...
let url = match std::env::args().nth(1) {
Some(url) => url,
None => {
println!("No CLI URL provided, using default.");
"https://hyper.rs".into()
}
};
println!("Status: {}", res.status());
eprintln!("Fetching {:?}...", url);
// reqwest::get() is a convenience function.
//
// In most cases, you should create/build a reqwest::Client and reuse
// it for all requests.
let res = reqwest::get(url).await?;
eprintln!("Response: {:?} {}", res.version(), res.status());
eprintln!("Headers: {:#?}\n", res.headers());
let body = res.text().await?;
println!("Body:\n\n{}", body);
println!("{}", body);
Ok(())
}