[![reqwest-badge]][reqwest] [![serde-badge]][serde] [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding]
reqwest::get
查询 Github 的stargazers API v3,获取星号标记项目的所有用户的列表。reqwest::Response
用Response::json
反序列化为User
对象,而这对象实现了serde::Deserialize
。
#[macro_use]
extern crate serde_derive;
extern crate reqwest;
use reqwest::Error;
#[derive(Deserialize, Debug)]
struct User {
login: String,
id: u32,
}
fn main() -> Result<(), Error> {
let request_url = format!("https://api.github.com/repos/{owner}/{repo}/stargazers",
owner = "rust-lang-nursery",
repo = "rust-cookbook");
println!("{}", request_url);
let mut response = reqwest::get(&request_url)?;
let users: Vec<User> = response.json()?;
println!("{:?}", users);
Ok(())
}