Examples: allow passing URL via CLI
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user