Python (Scripting) - Resolve SSLError CERTIFICATE_VERIFY_FAILED
by
Jeremy Canfield |
Updated: October 11 2023
| Python (Scripting) articles
Let's say something like this is being returned.
HTTPSConnectionPool(host='foo.example.com', port=443):
Max retries exceeded with url: /api/v1/bar
(Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:877)'),))
I typically happen upon this when using requests to submit a GET or POST or PATCH request to an API. This should only happen when targetting an API using a secure protocol such as HTTPS, perhaps like this.
#!/usr/bin/python
import requests
try:
response = requests.post(
"https://foo.example.com/api/v1/bar",
headers={ "Content-Type": "application/json" },
data={ "hello": "world" }
)
except Exception as exception:
print(exception)
else:
print("requests.post appears to have been successful")
If the API is trusted, such as when submitting the request to an API in your private network, I typically just add verify=False to the GET or POST or PATCH request.
#!/usr/bin/python
import requests
requests.post(
"https://foo.example.com/api/v1/bar",
headers={ "Content-Type": "application/json" },
data={ "hello": "world" },
verify=False
)
Did you find this article helpful?
If so, consider buying me a coffee over at