Skip to content

Instantly share code, notes, and snippets.

@saggit
Forked from fredhsu/eapi.rs
Created January 19, 2018 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saggit/54aba50e9df6e1b98aa97c5ff3606f6e to your computer and use it in GitHub Desktop.
Save saggit/54aba50e9df6e1b98aa97c5ff3606f6e 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