Created
August 30, 2023 13:55
-
-
Save harssh/f24f623b1db3770e3a40c789c975e442 to your computer and use it in GitHub Desktop.
Add certificate and key in Rust API request
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
//https://www.reddit.com/r/learnrust/comments/y5qm0y/certificates_in_reqwest_crate/ | |
let mut headers = HeaderMap::new(); | |
headers.insert(ACCEPT, "application/json".parse().unwrap()); | |
headers.insert("X-Application", "someKey".parse().unwrap()); | |
// Format body | |
let login_params = [("username", "myusername"), ("password", "password")]; | |
// create a certificate identity | |
let mut buf = Vec::new(); | |
File::open("client-2048.crt")?.read_to_end(&mut buf)?; | |
File::open("client-2048.key")?.read_to_end(&mut buf)?; | |
let id = reqwest::Identity::from_pem(&buf)?; | |
let client = reqwest::Client::builder() | |
.use_rustls_tls() | |
.identity(id) | |
.build()?; | |
let res = client | |
.post("https://someurl.com/api/certlogin") | |
.headers(headers) | |
.form(&login_params) | |
.send() | |
.await? | |
.json::<serde_json::Value>() | |
.await?; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://docs.rs/reqwest/latest/reqwest/tls/struct.Certificate.html
https://stackoverflow.com/questions/44059266/how-to-make-a-request-with-client-certificate-in-rust