Python requests - print entire http request (raw)?

While using the requests module, is there any way to print the raw HTTP request?

I don't want just the headers, I want the request line, headers, and content printout. Is it possible to see what ultimately is constructed from HTTP request?

Since v1.2.3 Requests added the PreparedRequest object. As per the documentation "it contains the exact bytes that will be sent to the server".

One can use this to pretty print a request, like so:

    import requests

    req = requests.Request('POST','',headers={'X-Custom':'Test'},data='a=1&b=2')
    prepared = req.prepare()

    def pretty_print_POST(req):
        At this point it is completely built and ready
        to be fired; it is "prepared".

        However pay attention at the formatting used in 
        this function because it is programmed to be pretty 
        printed and may differ from the actual request.
            req.method + ' ' + req.url,
            '\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),


which produces:

    Content-Length: 7
    X-Custom: Test


Then you can send the actual request with this:

    s = requests.Session()

These links are to the latest documentation available, so they might change in content: Advanced - Prepared requests and API - Lower level classes


Back to homepage or read more recommendations: