上有传参下传json的接口调用

上有传参下传json的接口调用

1、优化Myrequest

import requests
from conf.setting import log class MyRequest():
@staticmethod
def post(url,data=None,cookie=None,header=None,is_json=False,files=None,params=None):
data = data if data else {}
cookie = cookie if cookie else {}
header = header if header else {}
files = files if files else {}
try:
if is_json:
res = requests.post(url,json=data,cookies= cookie,headers = header,verify=False,files=files,params=params).json()
else:
res = requests.post(url, data=data, cookies=cookie, headers=header,verify=False,files=files,params=params).json()
log.debug('【接口返回数据:%s】'%res)
print('res...',res)
except Exception as e:
res = {'error':str(e)} #如果接口调用出错的话,那么就返回一个有错误信息的字典
log.error('异常信息:接口调用失败! url 【%s】 data 【%s】 实际结果是 【%s】'%(url,data,res))
return res @staticmethod
def get(url,data=None,cookie=None,header=None):
data = data if data else {}
cookie = cookie if cookie else {}
header = header if header else {}
try:
verify=False
res = requests.get(url, params=data, cookies=cookie, headers=header,verify=False).json()
log.debug('【接口返回数据:%s】'%res)
print('res...', res)
except Exception as e:
log.error('异常信息:接口调用失败! url 【%s】 data 【%s】'%(url,data))
res = {'error':str(e)} #如果接口调用出错的话,那么就返回一个有错误信息的字典
return res

  

2、上有传参下传json的接口调用

import unittest,requests
from lib.my_redis import my
# from lib.my_sql import my_sql
from conf.setting import BASE_URL
from urllib.parse import urljoin
from lib.my_request import MyRequest
from lib.tools import login class Pt_XXX(unittest.TestCase):
def test_c_XXXj(self):
'''用例描述'''
url = '/xxx/xxx/xxx/xxx'
real_url = urljoin(BASE_URL, url)
token = login()
params = {'xx': xxx,
'xx': 'xxxx'
} data={
"ts": xxx,
"id": "XXX"
} # res=requests.post(real_url,params=params,json=data).json()
res = MyRequest.post(real_url, data,is_json=True,params=params)
self.assertEqual(200, res.get('code'), msg='XXXX失败') if __name__ == '__main__':
c=Pt_XXX()
c.test_c_XXXj()
05-11 11:26