Deserialize JSON response.

This commit is contained in:
Lyle Mantooth 2023-05-13 15:38:07 -04:00
parent eb887d009d
commit a9d5459cb4
Signed by: IslandUsurper
GPG key ID: 6DB52EAE123A5789
3 changed files with 32 additions and 3 deletions

View file

@ -1,9 +1,22 @@
use serde::Deserialize;
#[tokio::main]
async fn main() {
let res = reqwest::get("https://api.thecatapi.com/v1/images/search")
.await
.unwrap();
println!("Status: {}", res.status());
let body = res.text().await.unwrap();
println!("Body: {}", body);
if !res.status().is_success() {
panic!("Request failed with HTTP {}", res.status());
}
#[derive(Deserialize)]
struct CatImage {
url: String,
}
let images: Vec<CatImage> = res.json().await.unwrap();
let image = images
.first()
.expect("the cat API should return at least one image");
println!("The image is at {}", image.url);
}