Skip to content

Commit

Permalink
Examples: allow passing URL via CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Jan 7, 2022
1 parent a03ca50 commit 7388b67
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
22 changes: 17 additions & 5 deletions examples/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
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());
println!("Headers:\n{:?}", res.headers());
// reqwest::blocking::get() is a convenience function.
//
// 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
res.copy_to(&mut std::io::stdout())?;

println!("\n\nDone.");
Ok(())
}
22 changes: 19 additions & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down

0 comments on commit 7388b67

Please sign in to comment.