Created
July 26, 2022 19:49
-
-
Save nicolasdanelon/76cd4fbc6378061f165c8cc50d09dc42 to your computer and use it in GitHub Desktop.
problem line 23
This file contains hidden or 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
[package] | |
name = "rust-json" | |
version = "0.1.0" | |
edition = "2021" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
serde = { version = "1.0", features = ["derive"] } | |
serde_json = "1.0" | |
reqwest = { version = "0.11", features = ["json"] } | |
tokio = { version = "1", features = ["full"] } |
This file contains hidden or 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 serde::{Deserialize, Serialize}; | |
use std::env; | |
#[derive(Debug, Serialize, Deserialize)] | |
struct Todo { | |
#[serde(rename = "userId")] | |
user_id: i32, | |
id: Option<i32>, | |
title: String, | |
completed: bool, | |
} | |
#[tokio::main] | |
async fn main() -> Result<(), reqwest::Error> { | |
let args: Vec<String> = env::args().collect(); | |
if args.len() < 1 { | |
println!("Missing parameter. Please run:"); | |
println!(" cargo run get"); | |
println!(" cargo run post"); | |
println!(" cargo run object"); | |
Ok(()); | |
} | |
let command = args[1].clone(); | |
match command.as_str() { | |
"get" => { | |
let todos:Vec<Todo> = reqwest::Client::new() | |
.get("https://jsonplaceholder.typicode.com/todos?userId=1") | |
.send() | |
.await? | |
.json() | |
.await?; | |
println!("{:#?}", todos); | |
}, | |
"post" => { | |
let new_todo = Todo { | |
user_id: 190, | |
id: None, | |
title: "this is just a test".to_owned(), | |
completed: false, | |
}; | |
let the_todo: Todo = reqwest::Client::new() | |
.post("https://jsonplaceholder.typicode.com/todos") | |
.json(&new_todo) | |
.send() | |
.await? | |
.json() | |
.await?; | |
println!("{:#?}", the_todo); | |
}, | |
"object" => { | |
let todos: serde_json::Value = reqwest::Client::new() | |
.post("https://jsonplaceholder.typicode.com/todos") | |
.json(&serde_json::json!({ | |
"userId": 1, | |
"title": "just another test".to_owned(), | |
"completed": false, | |
})) | |
.send() | |
.await? | |
.json() | |
.await?; | |
println!("{:#?}", todos); | |
}, | |
_ => { | |
println!("something went wrong"); | |
} | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment