python requests对请求封装工具类
1、这个工具类支持post方法的表单请求和json请求,get的请求,用这个可以满足平时基本的接口测试
import requests import json from apitest.resquestsTests import datas_tests class requestsUtils: def post_main(self, method,url, data, header): global res if method=="post": if header =="form-data": res = requests.post(url=url, data=data) if header=="Content-type:application/json": res = requests.post(url=url, json=data) return json.dumps(res.json(), ensure_ascii=False, sort_keys=True, indent=4) def get_main(self,method,url, data, header): global res if method=="get": if header != None: res = requests.get(url=url, data=data, headers=header) else: res = requests.get(url=url, data=data) return json.dumps(res.json(), ensure_ascii=False, sort_keys=True, indent=4) # if __name__ == '__main__': # print(requestsUtils().post_main(method=datas_tests.post_method,url=datas_tests.test_url+datas_tests.get_lujing, # data={"userid":datas_tests.uid,"activityid":datas_tests.activityid}, # header=datas_tests.header))