Last active
December 19, 2023 20:42
-
-
Save rahulkp220/5d7bda68aa4b66ac6f8737c1cd3234d4 to your computer and use it in GitHub Desktop.
Connect JSON Placeholder API with 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
use reqwest; | |
#[derive(serde::Deserialize, Debug)] | |
#[serde(rename_all = "camelCase")] | |
struct Todo { | |
user_id: i32, | |
id: i32, | |
title: String, | |
completed: bool, | |
} | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let client = reqwest::Client::builder() | |
.timeout(std::time::Duration::from_secs(10)) | |
.build() | |
.unwrap(); | |
// get one todo | |
let todo = client | |
.get("https://jsonplaceholder.typicode.com/todos/1") | |
.send() | |
.await? | |
.json::<Todo>() // notice the use of turbo fish syntax here | |
.await?; | |
// get multiple todos | |
let todos = client | |
.get("https://jsonplaceholder.typicode.com/todos") | |
.send() | |
.await? | |
.json::<Vec<Todo>>() // notice the use of turbo fish syntax here | |
.await?; | |
println!("{:?}", todo); | |
println!("{:?}", todos); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment