Utilities for Dumping Information About Responses

Occasionally, it is helpful to know almost exactly what data was sent to a server and what data was received. It can also be challenging at times to gather all of that data from requests because of all of the different places you may need to look to find it. In requests_toolbelt.utils.dump there are two functions that will return a bytearray with the information retrieved from a response object.

requests_toolbelt.utils.dump.dump_all(response, request_prefix=b'< ', response_prefix=b'> ')

Dump all requests and responses including redirects.

This takes the response returned by requests and will dump all request-response pairs in the redirect history in order followed by the final request-response.

Example:

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'))
Parameters:
  • response (requests.Response) – The response to format

  • request_prefix (bytes) – (optional) Bytes to prefix each line of the request data

  • response_prefix (bytes) – (optional) Bytes to prefix each line of the response data

Returns:

Formatted bytes of request and response information.

Return type:

bytearray

requests_toolbelt.utils.dump.dump_response(response, request_prefix=b'< ', response_prefix=b'> ', data_array=None)

Dump a single request-response cycle’s information.

This will take a response object and dump only the data that requests can see for that single request-response cycle.

Example:

import requests
from requests_toolbelt.utils import dump

resp = requests.get('https://api.github.com/users/sigmavirus24')
data = dump.dump_response(resp)
print(data.decode('utf-8'))
Parameters:
  • response (requests.Response) – The response to format

  • request_prefix (bytes) – (optional) Bytes to prefix each line of the request data

  • response_prefix (bytes) – (optional) Bytes to prefix each line of the response data

  • data_array (bytearray) – (optional) Bytearray to which we append the request-response cycle data

Returns:

Formatted bytes of request and response information.

Return type:

bytearray