https://blog.csdn.net/weixin_45912307/article/details/109517517
import requests
import json
import logging
from apitest.resquestsTests import datas_tests
class HttpRequest:
def http_request(self, method, url, param, header):
global res
if method.upper() == "POST":
if header == "Content-type:multipart/form-data":
try:
res = requests.post(url=url, data=param)
logging.info('post的form-data数据类型请求...')
except Exception as e:
logging.error('post的form-data数据类型请求异常为:', e)
if header == "Content-type:application/json":
try:
res = requests.post(url=url, json=param)
logging.info('post的json数据类型请求...')
except Exception as e:
logging.error('post的json数据类型请求异常为:', e)
if header == "Content-type:application/x-www-form-urlencoded":
try:
res = requests.post(url, data=param)
logging.info('post请求的x-www-form-urlencoded数据类型请求...')
except Exception as e:
logging.error('post的x-www-form-urlencoded数据类型请求异常为:', e)
if method.upper() == 'GET':
if header is not None:
try:
res = requests.get(url=url, data=param, headers=header)
logging.info('带请求头的get请求...')
except Exception as e:
logging.error('带请求头的get请求异常为:', e)
else:
try:
res = requests.get(url=url, data=param)
logging.info('无请求头的get请求...')
except Exception as e:
logging.error('无请求的get请求异常为:', e)
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))