simple.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #![deny(warnings)]
  2. // This is using the `tokio` runtime. You'll need the following dependency:
  3. //
  4. // `tokio = { version = "1", features = ["full"] }`
  5. #[cfg(not(target_arch = "wasm32"))]
  6. #[tokio::main]
  7. async fn main() -> Result<(), reqwest::Error> {
  8. // Some simple CLI args requirements...
  9. let url = match std::env::args().nth(1) {
  10. Some(url) => url,
  11. None => {
  12. println!("No CLI URL provided, using default.");
  13. "https://hyper.rs".into()
  14. }
  15. };
  16. eprintln!("Fetching {:?}...", url);
  17. // reqwest::get() is a convenience function.
  18. //
  19. // In most cases, you should create/build a reqwest::Client and reuse
  20. // it for all requests.
  21. let res = reqwest::get(url).await?;
  22. eprintln!("Response: {:?} {}", res.version(), res.status());
  23. eprintln!("Headers: {:#?}\n", res.headers());
  24. let body = res.text().await?;
  25. println!("{}", body);
  26. Ok(())
  27. }
  28. // The [cfg(not(target_arch = "wasm32"))] above prevent building the tokio::main function
  29. // for wasm32 target, because tokio isn't compatible with wasm32.
  30. // If you aren't building for wasm32, you don't need that line.
  31. // The two lines below avoid the "'main' function not found" error when building for wasm32 target.
  32. #[cfg(target_arch = "wasm32")]
  33. fn main() {}