Calling an API With Client Certificate

Hello, Rustaceans. Learning Rust has been on my to-do list for a year or so now and I think I finally have a good excuse to take the dive. I've read the first few chapters of the book and built a few of the toy apps there.

I need to develop a very simple command line tool that will take a few user inputs, use them to structure a call to an API, and finally to write the JSON response object to a .csv file. It seems like this is a manageable first-time project for an experienced programmer/complete Rust noob (please let me know if I'm wrong about that!). That being said, I have a question:

I browsed the reqwest documentation but didn't see an example of calling an API where a client TSL certificate is required. The way this is done via cURL is this:

curl --cert certificate_file.pem:password https://www.example.com/some_protected_page

Can this be done with reqwest?

Yes, you need to create an Identity and pass it to the client builder via the identity configuration method.

Note that you may need to enable specific crate features to use this functionality, you do this by writing the following in your Cargo.toml.

[dependencies]
reqwest = { version = "0.11", features = ["native-tls"] }

Since you are writing a command-line tool and not e.g. a web server, I would additionally recommend that you use the blocking variant of reqwest instead of async/await. To do this, add blocking to the list of features and use the Client from the reqwest::blocking module instead.

4 Likes
  1. Open the Postman Preferences and click the Certificates tab.
  2. Click Add tellpizzahut
  3. In the Host field, add the hostname for your session authentication server.
  4. In the CRT field enter the full path of your PEM certificate file (or click Choose File to locate the file).

Thanks for the info but I actually know how to do this in Postman. I was wondering how to do this in Rust via the reqwest crate.