-
Notifications
You must be signed in to change notification settings - Fork 47
/
reqwest.rs
35 lines (29 loc) · 1 KB
/
reqwest.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
extern crate reqwest;
use std::collections::HashMap;
use std::io::{BufRead, BufReader};
fn main() {
// Make a GET request
let resp = reqwest::get("https://www.rust-lang.org").unwrap();
assert!(resp.status().is_success());
let lines = BufReader::new(resp)
.lines()
.filter_map(|l| l.ok())
.take(10);
for line in lines {
println!("{}", line);
}
// Make a POST request
let client = reqwest::Client::new().unwrap();
let res = client.post("http://httpbin.org/post").unwrap()
.body("the exact body that is sent")
.send();
// Convert to/from JSON automatically
let mut map = HashMap::new();
map.insert("lang", "rust");
map.insert("body", "json");
// This will POST a body of `{"lang":"rust","body":"json"}`
let client = reqwest::Client::new().unwrap();
let res = client.post("http://httpbin.org/post").unwrap()
.json(&map).unwrap()
.send();
}