Skip to content

Instantly share code, notes, and snippets.

@RGGH
Created December 1, 2022 17:49
Show Gist options
  • Save RGGH/5c7e36471da7e8a46d09656a18de609f to your computer and use it in GitHub Desktop.
Save RGGH/5c7e36471da7e8a46d09656a18de609f to your computer and use it in GitHub Desktop.
get request in rust
/* reqwests example in Rust
Credit : https://www.becomebetterprogrammer.com/rust-how-to-make-an-http-request/
*/
#[tokio::main]
async fn main() {
let fact = get_cat_fact().await;
println!("fact = {:#?}", fact);
}
async fn get_cat_fact() -> Result<String, Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let body = client
.get("https://catfact.ninja/fact")
.send()
.await?
.text()
.await?;
Ok(body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment