Skip to content

Instantly share code, notes, and snippets.

@yermulnik
Last active April 6, 2025 14:57
Show Gist options
  • Save yermulnik/55c46ee560f6693af380254e2d0198c1 to your computer and use it in GitHub Desktop.
Save yermulnik/55c46ee560f6693af380254e2d0198c1 to your computer and use it in GitHub Desktop.
Python: debug output for `requests`/`urllib3`
import requests
import logging
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
data = {
"key": "value",
"another_key": "value"
}
url = "https://host/api/v1/endpoint"
headers = {'Accept': 'application/json'}
#sess = requests.Session()
#sess.trust_env = False # https://github.com/psf/requests/issues/6928
#response = sess.post(url, data=data, headers=headers, verify=True)
os.environ['NETRC'] = '' # Another workaround for https://github.com/psf/requests/issues/6928
response = requests.post(url, data=data, headers=headers, verify=True)
print(response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment