Skip to content

Instantly share code, notes, and snippets.

@rahulkp220
Last active December 19, 2023 20:42
Show Gist options
  • Save rahulkp220/5d7bda68aa4b66ac6f8737c1cd3234d4 to your computer and use it in GitHub Desktop.
Save rahulkp220/5d7bda68aa4b66ac6f8737c1cd3234d4 to your computer and use it in GitHub Desktop.
Connect JSON Placeholder API with Rust
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