
前提下载接口测试报告的命令:pip install requests
GET无参请求:r = requests.get('http://www.baidu.com')
GET传参:
payload = {'key1': 'value1', 'key2': 'value2', 'key3': None} r = requests.get('http://www.baidu.com ', params=payload)POST请求:
payload = {'key1': 'value1', 'key2': 'value2'} r = requests.post("http://httpbin.org/post", data=payload)Requests响应:
r.status_code 响应状态码 r.heards 响应头 r.cookies 响应 cookies r.text 响应文本 r.json() 响应的字典类型数据 r. encoding 当前编码 r. content 以字节形式(二进制)返回 最常用的是根据响应状态码判断接口是否连通,经常用于做接口中断言判断 Requests扩充: 1 :添加等待时间 requests.get(url,timeout=1) # 超过等待时间则报错 2 :添加请求头信息 requests.get(url,headers=headers) # 设置请求头 3 :添加文件 requests.post(url, files=files) # 添加文件 文件传输: url = 'http://httpbin.org/post' files = {'file': open('report.xls', 'rb')} r = requests.post(url, files=files)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)