1

I run curl command, something like this:

curl --tlsv1.2 -k -i -X POST -d 'payload={<json-payload>}' https://url.com:/handles/handle1

It was working perfectly. Now I need to imitate this in python. Referring to this solution, I tried running this in python console:

>>> import requests
>>> data = 'payload={<json-payload>}'
>>> headers = {'Content-type':'application/json'}
>>> response = requests.post('https://url.com:/handles/handle',headers=headers,data=data)

But getting following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 116, inpost
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='url.com', port=443): Max retries exceeded with url: /handles/handle (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)'),))

How can I resolve this?

MsA
  • 2,599
  • 3
  • 22
  • 47
  • You're probably using a self-signed certificate for that service. Add `verify=False` to your request and try again. – rdas Jun 28 '19 at 12:35
  • Possible duplicate of [Python Requests throwing SSLError](https://stackoverflow.com/questions/10667960/python-requests-throwing-sslerror) – h4z3 Jun 28 '19 at 12:37

1 Answers1

2

For ignoring TLS errors, like -k (--insecure) in curl, you need to use the verify=False paramter.

And to pass the POST data, use a dict:

data = {'payload': <json-payload>}

Now your request becomes:

requests.post('https://url.com:/handles/handle', headers=headers, data=data, verify=False)

If you want your POST data to be JSON serialized, use the json parameter instead of data:

requests.post('https://url.com:/handles/handle', headers=headers, json=data, verify=False)
heemayl
  • 39,294
  • 7
  • 70
  • 76
  • Getting `/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py:851: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning)` – MsA Jul 01 '19 at 09:10
  • @anir Did you use `verify=False`? – heemayl Jul 01 '19 at 09:11
  • @anir That's weird. What `requests` version are you on? Also please add the full traceback to your question. – heemayl Jul 01 '19 at 10:26
  • I am running this in jupyter notebook hosted in our development environment. Unfortunately this is the only way we can do development directly in our dev environment. I am not able to get detailed stack trace. Is it possible to get it in jupyter? This is how it look currently: [image](https://i.postimg.cc/vmKTggRd/jupyter.png) – MsA Jul 03 '19 at 11:21