Python获取http请求响应头的headers

2021-08-09  本文已影响0人  Queenie的学习笔记
  1. 测试下载文件的接口时,我要拿到响应头的Content-Type为application/octet-stream;charset=utf-8类型的响应数据,
    之前用的是下面部分的代码,但这段获取响应头的headers的代码不对,获取到的rq.headers是HTTPHeaderDict,不是<class 'requests.models.Response'>,结果取到的Content-Type'一直是application/json;charset=utf-8:
import urllib3

http = urllib3.PoolManager()
tm = urllib3.Timeout(connect=1.0, read=3.0)
rq = http.request(method, url, headers=headers, timeout=tm, retries=5, redirect=4)  #这里的url和method需要自己指定
# print("响应码:", rq.status)
# print("响应头部:", rq.headers)
# print(type(rq.headers))
contentType = rq.headers.get('Content-Type')
  1. 正确的写法
res = self.run_method.run_main(method,url,params,data_json,headers)
ContentType = res.headers.get('Content-Type')
print("响应头的Content-Type格式:", ContentType)

参考来源:https://www.pythonf.cn/read/93693

上一篇下一篇

猜你喜欢

热点阅读