【问题标题】:how to print request body in urllib3 python如何在 urllib3 python 中打印请求正文
【发布时间】:2021-02-11 06:28:16
【问题描述】:

我可以使用 python 中的 urllib3 库成功发出多部分发布请求。但是如何在发出 POST 请求时打印请求正文?

python 请求库:我们有如下选项:print(response.request.body)

代码如下:

http = urllib3.PoolManager()

path_bdl = os.path.dirname(__file__) + '/some_bin_file.xyz'
content_json = os.path.dirname(__file__) + '/some_json.json'


with open(path_, 'rb') as fp:
    binary_data = fp.read()

with open(content_json, 'r') as j:
    json_data1 = j.read()

url = "//myurl"

headers = {}  # dictionary

response = http.request(
    'POST',
    url,
    fields={
        "abc": (content_json, json_data1, "application/json"),
        "bbb": (path_, binary_data, "xyz_Content_type")
    },
    headers=headers
)

print('\n StatusCode: ', response.status)
print(response.headers)
print(response.request) >>>>>>>>>>>> HTTPResponse has no attribute 'request'

【问题讨论】:

    标签: python json python-3.x python-requests urllib3


    【解决方案1】:

    来自docs

    HTTPResponse 对象提供状态、数据和标头属性

    尝试使用response.data 读取响应的数据部分。

    【讨论】:

      猜你喜欢
      • 2015-10-04
      • 1970-01-01
      • 2021-08-14
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多