-
-
Save saggit/54aba50e9df6e1b98aa97c5ff3606f6e to your computer and use it in GitHub Desktop.
Using Rust/Hyper to do a HTTP Post with JSON
This file contains 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