关于响应信息的转储实用程序

有时,了解发送到服务器和接收到的数据非常有用。有时,由于需要查找数据的不同位置,从请求中收集所有这些数据可能具有挑战性。在 requests_toolbelt.utils.dump 中,有两个函数将返回一个 bytearray,其中包含从响应对象中检索的信息。

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

转储所有请求和响应,包括重定向。

这将获取由请求返回的响应,并将重定向历史记录中的所有请求-响应对按顺序转储,然后是最终的请求-响应。

示例

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'))
参数:
  • response (requests.Response) – 要格式化的响应

  • request_prefix (bytes) –(可选)每个请求数据行的前缀字节

  • response_prefix (bytes) –(可选)每个响应数据行的前缀字节

返回:

请求和响应信息的格式化字节。

返回类型:

bytearray

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

转储单个请求-响应周期的信息。

这将获取一个响应对象,并仅转储请求针对该单个请求-响应周期可见的数据。

示例

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'))
参数:
  • response (requests.Response) – 要格式化的响应

  • request_prefix (bytes) –(可选)每个请求数据行的前缀字节

  • response_prefix (bytes) –(可选)每个响应数据行的前缀字节

  • data_array (bytearray) –(可选)我们将请求-响应周期数据追加到的字节数组

返回:

请求和响应信息的格式化字节。

返回类型:

bytearray