Created
July 6, 2016 15:53
-
-
Save Arzte/965585472973d9b45145920dae1cb7fe to your computer and use it in GitHub Desktop.
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
#![feature(plugin)] | |
#![plugin(serde_macros)] | |
#![feature(custom_derive)] | |
#![feature(custom_attribute)] | |
extern crate serde; | |
extern crate serde_json; | |
extern crate hyper; | |
extern crate url; | |
use url::Url; | |
fn main() { | |
// Construct the URL you want to access | |
let url = "http://catfacts-api.appspot.com/api/facts?number=1" | |
.parse::<Url>() | |
.expect("Unable to parse URL"); | |
// Initialize the Hyper client and make the request. | |
let client = hyper::Client::new(); | |
let mut response = client.get(url).send().unwrap(); | |
// Initialize a string buffer, and read the response into it. | |
let mut result = String::new(); | |
response.read_to_string(&mut result).unwrap(); | |
// Deserialize the result. | |
#[derive(Deserialize)] | |
pub struct CatFacts { | |
pub facts: Vec<String>, | |
pub success: bool, | |
} | |
let cat_facts = serde_json::from_str::<CatFacts>(&result).unwrap().facts; | |
println("{:?}", cat_facts); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment