Created
December 1, 2022 17:49
-
-
Save RGGH/5c7e36471da7e8a46d09656a18de609f to your computer and use it in GitHub Desktop.
get request in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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