Last active
August 9, 2022 18:41
-
-
Save j-griffith/4fbab8721e6d73234907754436b95715 to your computer and use it in GitHub Desktop.
Example PUT/GET of Secret to Vault API Using Reqwest
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
use reqwest::header::HeaderMap; | |
use serde::{Deserialize, Serialize}; | |
use std::collections::HashMap; | |
#[derive(Serialize, Deserialize, Debug)] | |
struct Secret { | |
data: HashMap<String, String>, | |
} | |
#[derive(Serialize, Deserialize)] | |
struct VaultResponse { | |
request_id: String, | |
lease_id: String, | |
data: Secret, | |
} | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let mut s = HashMap::new(); | |
s.insert("biz".to_string(), "baz".to_string()); | |
let sec = Secret { data: s }; | |
let mut headers = HeaderMap::new(); | |
headers.insert( | |
"X-Vault-Token", | |
"hvs.iJd4KhkIu3Zlo3Y3JttPmsvu".parse().unwrap(), | |
); | |
headers.insert("CONTENT_TYPE", "application/json".parse().unwrap()); | |
//let client = reqwest::Client::new(); | |
let client = reqwest::Client::builder() | |
.default_headers(headers) | |
.build()?; | |
let res = client | |
.post("http://127.0.0.1:8200/v1/secret/data/secret-1") | |
.json(&sec) | |
.send() | |
.await; | |
println!("POST Response: {:?}", res); | |
let res = client | |
.get("http://127.0.0.1:8200/v1/secret/data/secret-5") | |
.send() | |
.await | |
.expect("failed api get call") | |
.json::<VaultResponse>() | |
.await | |
.expect("failed api get response"); | |
println!("Response: {:#?}", res.data.data); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment