WRITELOOP

PYTHON - HOW TO MAKE REQUESTS TO URLS WITH SELF-SIGNED CERTIFICATES

2020 March 12

You want to make a https request to an URL that has a self-signed certificate, and you’re using the requests library on your python code. By default, it will refuse to do that. To fix this problem, first you have to add the certificate to your SO (if you are on a container, add it at some point on the Dockerfile). If you are on Ubuntu e.g.:

  1. Install the packages below:
$ apt-get install -y --no-install-recommends ca-certificates openssl
  1. Copy (or download) the self-signed certificate crt file to the ca-certificates store:
$ cp custom.crt /usr/local/share/ca-certificates
  1. Update the certificates registry:
$ update-ca-certificates
  1. Check the request working fine with curl:
$ curl https://my-custom-domain.com

IMPORTANT: it should work now, do NOT use -k. If you use it, you are bypassing the ssl verification anyway. 5) If you are using e.g. requests on python to make your request, you must use the parameter verify, passing the full path to the custom certificate. E.g.:

requests.get('https://my-custom-domain.com', verify='/usr/local/share/ca-certificates/custom.crt')

reference: https://stackoverflow.com/questions/42982143/python-requests-how-to-use-system-ca-certificates-debian-ubuntu