I'm new to Rust and am also trying to learn to make API calls with reqwest
. I was wondering if someone would be kind enough to show me what the blocking version of the below code would look like?
extern crate reqwest;
extern crate tokio;
use serde::{Serialize, Deserialize};
use serde_json;
#[derive(Deserialize, Serialize, Debug)]
struct CatFact {
text: String
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url = "https://cat-fact.herokuapp.com/facts";
let res = reqwest::get(url).await?;
let json = res.text().await.unwrap();
let facts: Vec<CatFact> = serde_json::from_str(&json).unwrap();
println!("facts = {:#?}", facts);
Ok(())
}
Also, why is it necessary to call the await
method on res.text()
in this case? What exactly is going on there?