Skip to content

Instantly share code, notes, and snippets.

@fredhsu
Created June 23, 2015 21:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save fredhsu/ae4088f905ac25bfcc04 to your computer and use it in GitHub Desktop.
Save fredhsu/ae4088f905ac25bfcc04 to your computer and use it in GitHub Desktop.
Using Rust/Hyper to do a HTTP Post with JSON
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