As others have noted there is a nice requests-toolbelt
module with convenient functions to dump request and response content using requests hooks. Unfortunately (as of now) there is only a hook to be invoked on a successful completion of a request. It's not always a case. I.e. request could end up with ConnectionError
or Timeout
exceptions.
The requests-toolbelt
module on it's own also provides public functions to dump completed requests only. However using a bit of non-public APIs and Session sub-classing it's possible to implement logging of requests before send and logging of responses after receive.
NOTE: code relies on implementation details/non-public APIs of requests-toolbelt
module and thus make unexpectedly break in the future:
import requests
from requests_toolbelt.utils import dump
class MySession(requests.Session):
def send(self, req, *args, **kwargs):
prefixes = dump.PrefixSettings(b'< ', b'> ')
data = bytearray()
try:
dump._dump_request_data(req, prefixes, data)
resp = super().send(req, *args, **kwargs)
dump._dump_response_data(resp, prefixes, data)
finally:
print(data.decode('utf-8'))
return resp
and here is an example of usage:
>>> MySession().get('https://httpbin.org/headers')
< GET /headers HTTP/1.1
< Host: httpbin.org
< User-Agent: python-requests/2.25.1
< Accept-Encoding: gzip, deflate
< Accept: */*
< Connection: keep-alive
<
> HTTP/1.1 200 OK
> Date: Fri, 19 Aug 2022 10:43:51 GMT
> Content-Type: application/json
> Content-Length: 225
> Connection: keep-alive
> Server: gunicorn/19.9.0
> Access-Control-Allow-Origin: *
> Access-Control-Allow-Credentials: true
>
{
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.25.1"
}
}
>>> MySession().get('https://non.existent')
< GET / HTTP/1.1
< Host: non.existent
< User-Agent: python-requests/2.25.1
< Accept-Encoding: gzip, deflate
< Accept: */*
< Connection: keep-alive
<
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 169, in _new_conn
conn = connection.create_connection(
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 73, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "/usr/lib/python3.10/socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
...