import json import requests requests.packages.urllib3.disable_warnings()
class RunMethod: def post_main(self, url, data, header=None): res = None if header != None: res = requests.post(url=url, data=data, headers=header, verify=False) else: res = requests.post(url=url, data=data, verify=False) return res.json()
def get_main(self, url, data=None, header=None): res = None if header != None: res = requests.get(url=url, params=data, headers=header, verify=False) else: res = requests.get(url=url, params=data, verify=False) return res.json()
def run_main(self, method, url, data=None, header=None): res = None if method == 'Post': res = self.post_main(url, data, header) else: res = self.get_main(url, data, header) return json.dumps(res, indent=2, sort_keys=True, ensure_ascii=False)
if __name__ == '__main__': url = '要测试接口的登录名称' header = { 'Content-Type': 'application/json;charset=UTF-8', 'User - Agent': 'Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 77.0.3865.90Safari / 537.36', 'Cookie': 'JSESSIONID = A_u - 1Fehp5DAb0KmmZ_Se0_njZbhoD3VbNuQq3zD;user = {}' } data = { 'loginName': '登录账号', 'password': '登录密码' } run = RunMethod() run_test = run.run_main(method="Post", url=url, header=header,data=data) print(run_test)
打印的结果是:
解决方法:
这样就能正确的把这个接口里面的数据给打印出来了
|