Skip to content

Instantly share code, notes, and snippets.

@Arzte
Created July 6, 2016 15:53
Show Gist options
  • Save Arzte/965585472973d9b45145920dae1cb7fe to your computer and use it in GitHub Desktop.
Save Arzte/965585472973d9b45145920dae1cb7fe to your computer and use it in GitHub Desktop.
#![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