mirror of
https://github.com/seanmonstar/reqwest.git
synced 2026-10-28 17:05:17 +08:00
This makes the code a bit easier to read, and might result in a few minor perf optimizations (e.g. if Display trait is implemented, it might be better to make it part of the formatting)
41 lines
1.2 KiB
Rust
41 lines
1.2 KiB
Rust
#![deny(warnings)]
|
|
|
|
// This is using the `tokio` runtime. You'll need the following dependency:
|
|
//
|
|
// `tokio = { version = "1", features = ["full"] }`
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), reqwest::Error> {
|
|
// Some simple CLI args requirements...
|
|
let url = if let Some(url) = std::env::args().nth(1) {
|
|
url
|
|
} else {
|
|
println!("No CLI URL provided, using default.");
|
|
"https://hyper.rs".into()
|
|
};
|
|
|
|
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}");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
// The [cfg(not(target_arch = "wasm32"))] above prevent building the tokio::main function
|
|
// for wasm32 target, because tokio isn't compatible with wasm32.
|
|
// If you aren't building for wasm32, you don't need that line.
|
|
// The two lines below avoid the "'main' function not found" error when building for wasm32 target.
|
|
#[cfg(target_arch = "wasm32")]
|
|
fn main() {}
|