Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 1.3 KB

rest-get.zh.md

File metadata and controls

35 lines (28 loc) · 1.3 KB

查询 GitHub API

[![reqwest-badge]][reqwest] [![serde-badge]][serde] [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding]

reqwest::get查询 Github 的stargazers API v3,获取星号标记项目的所有用户的列表。reqwest::ResponseResponse::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(())
}