Skip to content

Instantly share code, notes, and snippets.

@dheffx
Created February 11, 2018 21:50
Show Gist options
  • Select an option

  • Save dheffx/056829d93afb3ae79adc40fb91e754eb to your computer and use it in GitHub Desktop.

Select an option

Save dheffx/056829d93afb3ae79adc40fb91e754eb to your computer and use it in GitHub Desktop.
rust-http-json-example
extern crate reqwest;
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
use std::io::Read;
use serde_json::Error;
#[derive(Serialize, Deserialize)]
#[derive(Debug)]
struct User {
login: String,
id: u32,
}
fn get_user() -> Result<User, Error> {
let mut resp = reqwest::get("https://api.github.com/users/dheffx").unwrap();
assert!(resp.status().is_success());
let mut content = String::new();
resp.read_to_string(&mut content);
let u: User = serde_json::from_str(content.as_str())?;
Ok(u)
}
fn main() {
let u: User = get_user().unwrap();
println!("{:?}", u);
println!("XXX {}", u.login);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment