Python requests - print entire http request (raw)? Python requests - print entire http request (raw)? python python

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 requestsreq = 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.    """    print('{}\n{}\r\n{}\r\n\r\n{}'.format(        '-----------START-----------',        req.method + ' ' + req.url,        '\r\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),        req.body,    ))pretty_print_POST(prepared)

which produces:

-----------START-----------POST 7X-Custom: Testa=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 requestsresponse ='', 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.

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.

It's as easy as this:

import requestsfrom requests_toolbelt.utils import dumpresp = requests.get('')data = dump.dump_all(resp)print(data.decode('utf-8'))


You can simply install it by typing:

pip install requests_toolbelt