Created
June 23, 2015 21:24
-
-
Save fredhsu/ae4088f905ac25bfcc04 to your computer and use it in GitHub Desktop.
Using Rust/Hyper to do a HTTP Post with JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate hyper; | |
extern crate core; | |
use std::io::Read; | |
use hyper::Client; | |
use hyper::header::Connection; | |
use hyper::header::Basic; | |
use hyper::header::Headers; | |
use core::str::FromStr; | |
fn main() { | |
// Create a client. | |
let mut client = Client::new(); | |
let auth = Basic::from_str("admin:admin").unwrap(); | |
let mut res = client.post("https://admin:admin@bleaf1/command-api") | |
// set a header | |
.header(auth) | |
.body(r#"{ | |
"jsonrpc": "2.0", | |
"method": "runCmds", | |
"params": { | |
"version": 1, | |
"cmds": [ | |
"show version" | |
], | |
"format": "json", | |
}, | |
"id": "1" | |
}"#) | |
// let 'er go! | |
.send().unwrap(); | |
// Read the Response. | |
let mut body = String::new(); | |
res.read_to_string(&mut body).unwrap(); | |
println!("Response: {}", body); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment