问答 使用 python-request 做接口测试,header 参数有默认值吗

东方不败 · October 16, 2018 · Last by 陈子昂 replied at October 17, 2018 · 3359 hits

使用 python-request 做接口测试,request 中 post 和 get 方法都可以不使用 header 参数,那么 header 会有默认值吗?

共收到 4 条回复 时间 点赞

默认会添加 UserAgent Content-Type Content-Length Host,抓个包就能看到

requests.get('http://www.baidu.com').request.headers

try:
s = Session()
req = Request('GET', url, headers={'Host':"www.test.com"})
prepped = req.prepare()
resp = s.send(prepped)
if resp.status_code == 200:
print resp.text
else:
return None
except Exception:
print traceback.format_exc()
return None

...header http 族的头 还是要带的吧 post 和 get 每次都需要传入这个头,你可以写成数据驱动,有时候 header 里面内容会不一样。

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up