I have the below working in Bash. How would this be written in Python using requests or pycurl?
curl -o .output_data.xml -v --cert ../cert/my_cert.pem:password123 -k 'https://my.target.url' -H 'Content-Type: application/json' -d '{"document":{"header":{"exportType":"XML"}}}'
Thanks to the comment from Tripleee regarding https://github.com/psf/requests/issues/1573#issuecomment-169916326 the below snippet now works:
I just needed to map the cURL option arguments correctly:
--cert maps to c.setopt(pycurl.SSLCERT, cert_file_path) and c.setopt(pycurl.SSLKEYPASSWD, "password123")
-k maps to c.setopt(pycurl.SSL_VERIFYPEER, False)
-H maps to c.setopt(pycurl.HTTPHEADER,["Content-Type: application/json"])
-d maps to c.setopt(pycurl.POSTFIELDS, json.dumps(params))
there is no mapping for -o
so I capture the output using a buffer c.setopt(b.WRITEFUNCTION, b.write)
.
b.getvalue()
will allow me to later parse the data from the captured XML string using Element Tree or similar.
import pycurl
import io
import json
cert_file_path = "../cert/my_cert.pem"
url = "https://my.target.url"
params={"document":{"header":{"exportType":"XML"}}}
b = io.BytesIO()
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.SSLCERT, cert_file_path)
c.setopt(pycurl.SSLKEYPASSWD, "password123")
c.setopt(pycurl.SSL_VERIFYPEER, False)
c.setopt(pycurl.HTTPHEADER,["Content-Type: application/json"])
c.setopt(pycurl.POSTFIELDS, json.dumps(params))
c.setopt(c.WRITEFUNCTION, b.write)
c.perform()
xml_string = b.getvalue().decode('UTF-8')