HTTPS/Client-authenticated: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Removed trans template as not applicable here.)
(Add Rust version)
Line 268: Line 268:
http.request request
http.request request
end</lang>
end</lang>
=={{header|Rust}}==
{{works with|Rust|2021}}
This implementation uses [https://crates.io/crates/reqwest reqwest], the de facto standard high-level HTTP(S) rust library. It is roughly equivalent in purpose and functionality to Python's [https://docs.python-requests.org/en/master/index.html requests].
===Cargo.toml dependencies===
The blocking variant of the reqwest library is used here for simplicity's sake. An asynchronous API is also available.

Native (system) TLS libraries are used instead of Rustls, the Rust TLS implementation, because we use a PKCS#12 certificate which at the time of writing does not seem to be available on Rustls. A PKCS#12 certificate is used instead of its PEM equivalent because reading password-protected PEM files [https://docs.rs/reqwest/0.11.6/reqwest/tls/struct.Identity.html#method.from_pem does not seem to be available] either.
<lang toml>reqwest = {version = "0.11", features = ["native-tls", "blocking"]}</lang>
===src/main.rs===
<lang rust>use std::fs::File;
use std::io::Read;

use reqwest::blocking::Client;
use reqwest::Identity;

fn main() -> std::io::Result<()> {
let identity = {
let mut buf = Vec::new();

// Downloaded from https://badssl.com/certs/badssl.com-client.p12
File::open("badssl.com-client.p12")?.read_to_end(&mut buf)?;

// Password is badssl.com
Identity::from_pkcs12_der(&buf, "badssl.com").unwrap()
};

let client = Client::builder().identity(identity).build().unwrap();
let response = client.get("https://client.badssl.com/").send().unwrap();

if !response.status().is_success() {
eprintln!("HTTP error requesting URL: {}", response.status());
}

println!("Got response from server: {}", response.text().unwrap());

Ok(())
}</lang>


=={{header|Scala}}==
=={{header|Scala}}==