Skip to navigation
Load a JSON URL String and parse it automatically with rust
01.06.23
Example: Check your Website Performance via googles pagespeed https://developers.google.com/speed/docs/insights/v5/get-started?hl=en ## Cargo.tom ```yaml [package] name = "speedtest" version = "0.1.0" edition = "2021" [dependencies] curl = {"version"= "0.4.44"} json = {"version"= "0.12.4"} ``` ## src/main.rs ```rust use curl::easy::Easy; use std::str; fn speed_test() -> String { println!("....speed test"); let url = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://www.myridia.com&category=performance"; let mut data: Vec
= Vec::new(); let mut handle = Easy::new(); handle.ssl_verify_peer(false); handle.url(url).unwrap(); { let mut transfer = handle.transfer(); transfer .write_function(|new_data| { data.extend_from_slice(new_data); Ok(new_data.len()) }) .unwrap(); transfer.perform().unwrap(); } let s = str::from_utf8(&data).unwrap(); //println!("...String Result: {:?}", s); let o = json::parse(s).unwrap(); //println!("...Parsed Object: {:?}", o); let result = &o["lighthouseResult"]["categories"]["performance"]["score"]; return result.to_string(); } fn main() { println!("....main"); let r = speed_test(); println!("Performance: {}", r); } ``` ## Library Structure ```text speedtest v0.1.0 (/home/veto/webs/speedtest/body) ├── curl v0.4.44 │ ├── curl-sys v0.4.62+curl-8.1.0 │ │ ├── libc v0.2.144 │ │ ├── libz-sys v1.1.9 │ │ │ └── libc v0.2.144 │ │ │ [build-dependencies] │ │ │ ├── cc v1.0.79 │ │ │ └── pkg-config v0.3.27 │ │ └── openssl-sys v0.9.88 │ │ └── libc v0.2.144 │ │ [build-dependencies] │ │ ├── cc v1.0.79 │ │ └── pkg-config v0.3.27 │ │ [build-dependencies] │ │ ├── cc v1.0.79 │ │ └── pkg-config v0.3.27 │ ├── libc v0.2.144 │ ├── openssl-probe v0.1.5 │ ├── openssl-sys v0.9.88 (*) │ └── socket2 v0.4.9 │ └── libc v0.2.144 └── json v0.12.4 ```
https://docs.rs/curl/0.4.44/curl/easy/struct.Easy.html#
Reply
Anonymous
Information Epoch 1732425652
Worse is better.
Home
Notebook
Contact us