Python requests – print entire http request (raw)?

Python requests – print entire http request (raw)?

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,http://stackoverflow.com,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.
    
    print({}n{}rn{}rnrn{}.format(
        -----------START-----------,
        req.method +   + req.url,
        rn.join({}: {}.format(k, v) for k, v in req.headers.items()),
        req.body,
    ))

pretty_print_POST(prepared)

which produces:

-----------START-----------
POST http://stackoverflow.com/
Content-Length: 7
X-Custom: Test

a=1&b=2

Then you can send the actual request with this:

s = requests.Session()
s.send(prepared)

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

import requests

response = requests.post(http://httpbin.org/post, data={key1:value1})
print(response.request.url)
print(response.request.body)
print(response.request.headers)

Response objects have a .request property which is the original PreparedRequest object that was sent.

Python requests – print entire http request (raw)?

An even better idea is to use the requests_toolbelt library, which can dump out both requests and responses as strings for you to print to the console. It handles all the tricky cases with files and encodings which the above solution does not handle well.

Its as easy as this:

import requests
from requests_toolbelt.utils import dump

resp = requests.get(https://httpbin.org/redirect/5)
data = dump.dump_all(resp)
print(data.decode(utf-8))

Source: https://toolbelt.readthedocs.org/en/latest/dumputils.html

You can simply install it by typing:

pip install requests_toolbelt

Leave a Reply

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