mHelpMe
mHelpMe

Reputation: 6668

using requests.post to send a request with a certificate

I am trying to send some data via a web based api. The client has given me an example snippet of code to test everything.

My problem below is how to use my certificate.

Their example

response = requests.post(url, data=data, headers=headers, cert=('certificate.cer','keyfile.key'))

I tried

response = requests.post(url, data=data, headers=headers, cert=('C:\MyPath\My-certs.p12','password'))

However I get the error,

OSError: Could not find the TLS key file, invalid path: password

I have written a C# example and the code works, so I know the certificate and the other pieces are ok. I just can't get the certificate to work in python

Upvotes: 1

Views: 2406

Answers (1)

Asad
Asad

Reputation: 219

Your certificate file is P12, which isn't supported by requests yet. You have two options:

  1. Convert the P12 file into separate public certificate and private key files. For Linux or Mac, use this. Then you can use the separate files as a tuple in cert.

  2. Use requests-pkcs12 which has support for P12 files:

--

from requests_pkcs12 import post
response = post(url, data=data, headers=headers, pkcs12_filename='C:\MyPath\My-certs.p12', pkcs12_password='password')

Upvotes: 2

Related Questions