Examples: allow passing URL via CLI
This commit is contained in:
@@ -4,16 +4,28 @@
|
|||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
println!("GET https://www.rust-lang.org");
|
// 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()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let mut res = reqwest::blocking::get("https://www.rust-lang.org/")?;
|
eprintln!("Fetching {:?}...", url);
|
||||||
|
|
||||||
println!("Status: {}", res.status());
|
// reqwest::blocking::get() is a convenience function.
|
||||||
println!("Headers:\n{:?}", res.headers());
|
//
|
||||||
|
// In most cases, you should create/build a reqwest::Client and reuse
|
||||||
|
// it for all requests.
|
||||||
|
let mut res = reqwest::blocking::get(url)?;
|
||||||
|
|
||||||
|
eprintln!("Response: {:?} {}", res.version(), res.status());
|
||||||
|
eprintln!("Headers: {:#?}\n", res.headers());
|
||||||
|
|
||||||
// copy the response body directly to stdout
|
// copy the response body directly to stdout
|
||||||
res.copy_to(&mut std::io::stdout())?;
|
res.copy_to(&mut std::io::stdout())?;
|
||||||
|
|
||||||
println!("\n\nDone.");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,29 @@
|
|||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), reqwest::Error> {
|
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?;
|
let body = res.text().await?;
|
||||||
|
|
||||||
println!("Body:\n\n{}", body);
|
println!("{}", body);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user