2

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?

2
  • You're probably using a self-signed certificate for that service. Add verify=False to your request and try again. Commented Jun 28, 2019 at 12:35
  • Possible duplicate of Python Requests throwing SSLError Commented Jun 28, 2019 at 12:37

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

4 Comments

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)
@anir Did you use verify=False?
@anir That's weird. What requests version are you on? Also please add the full traceback to your question.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.