我正在尝试使用 Hyper 库在 Rust 中向 Github 的 API 发出 GET 请求,并在标头中使用用户代理字符串。我没有运气编译.header(UserAgent("string"))
。有人愿意提出一种惯用的方式来完成我想要的吗?
extern crate hyper;
use std::io::Read;
use hyper::Client;
use hyper::header::{Connection, Headers};
struct Subtasks {
github: &'static str,
}
struct Tasks {
rust: Subtasks,
go: Subtasks,
}
fn main() {
// initialize struct and populate
let tasks = Tasks {
rust: Subtasks {
github: "https://api.github.com/rust-lang/rust",
},
go: Subtasks {
github: "https://api.github.com/golang/go",
},
};
let client = Client::new();
let mut result = client.get(tasks.rust.github)
.header(Connection::close())
.send()
.unwrap();
let mut body = String::new();
result.read_to_string(&mut body).unwrap();
println!("Response: {}", body);
}