debugging – How can I see the entire HTTP request thats being sent by my Python application?

debugging – How can I see the entire HTTP request thats being sent by my Python application?

A simple method: enable logging in recent versions of Requests (1.x and higher.)

Requests uses the http.client and logging module configuration to control logging verbosity, as described here.


Code excerpted from the linked documentation:

import requests
import logging

# These two lines enable debugging at httplib level (requests->urllib3->http.client)
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# The only thing missing will be the response.body which is not logged.
    import http.client as http_client
except ImportError:
    # Python 2
    import httplib as http_client
http_client.HTTPConnection.debuglevel = 1

# You must initialize logging, otherwise youll not see debug output.
requests_log = logging.getLogger(requests.packages.urllib3)
requests_log.propagate = True


Example Output

$ python 
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1):
send: GET /headers HTTP/1.1rnHost: httpbin.orgrnAccept-Encoding: gzip, deflate, compressrnAccept: */*rnUser-Agent: python-requests/1.2.0 CPython/2.7.3 Linux/3.2.0-48-genericrnrn
reply: HTTP/1.1 200 OKrn
header: Content-Type: application/json
header: Date: Sat, 29 Jun 2013 11:19:34 GMT
header: Server: gunicorn/0.17.4
header: Content-Length: 226
header: Connection: keep-alive
DEBUG:requests.packages.urllib3.connectionpool:GET /headers HTTP/1.1 200 226
r = requests.get(, auth=(user, pass))

r is a response. It has a request attribute which has the information you need.

r.request.allow_redirects  r.request.headers          r.request.register_hook
r.request.auth             r.request.hooks            r.request.response
r.request.cert             r.request.method           r.request.send
r.request.config           r.request.params           r.request.sent
r.request.cookies          r.request.path_url         r.request.session             r.request.prefetch         r.request.timeout
r.request.deregister_hook  r.request.proxies          r.request.url
r.request.files            r.request.redirect         r.request.verify

r.request.headers gives the headers:

{Accept: */*,
 Accept-Encoding: identity, deflate, compress, gzip,
 Authorization: uBasic dXNlcjpwYXNz,
 User-Agent: python-requests/0.12.1}

Then has the body as a mapping. You can convert this with urllib.urlencode if they prefer:

import urllib
b =
encoded_body = urllib.urlencode(b)

depending on the type of the response the .data-attribute may be missing and a .body-attribute be there instead.

debugging – How can I see the entire HTTP request thats being sent by my Python application?

You can use HTTP Toolkit to do exactly this.

Its especially useful if you need to do this quickly, with no code changes: you can open a terminal from HTTP Toolkit, run any Python code from there as normal, and youll be able to see the full content of every HTTP/HTTPS request immediately.

Theres a free version that can do everything you need, and its 100% open source.

Im the creator of HTTP Toolkit; I actually built it myself to solve the exact same problem for me a while back! I too was trying to debug a payment integration, but their SDK didnt work, I couldnt tell why, and I needed to know what was actually going on to properly fix it. Its very frustrating, but being able to see the raw traffic really helps.

Leave a Reply

Your email address will not be published. Required fields are marked *