优化表单请求提交

This commit is contained in:
jing song 2022-04-11 14:48:01 +08:00
parent cb3dea8a06
commit 3712c8318b
1 changed files with 18 additions and 8 deletions

View File

@ -9,7 +9,7 @@ from requests_toolbelt import MultipartEncoder
from util.tools.log import Log
from util.tools.randomData import replace_random
from config.confManage import host_manage
from util.tools.caches import Cache
from util.tools.caches import Cache, valueHandle
Log()
@ -62,16 +62,16 @@ class apiSend(object):
allure.attach(name="请求头", body=str(header))
allure.attach(name="请求参数", body=str(data_random))
if "multipart/form-data" in str(header.values()):
# 根据请求头contenttype判断是否为上传文件
if isinstance(files, dict):
for i in files:
value = files[i]
for fileskey in files:
value = files[fileskey]
if isinstance(value, int):
files[i] = str(value)
files[fileskey] = str(value)
pass
elif '/' in value or "\\" in value:
file_parm = i
file_parm = fileskey
files[file_parm] = (os.path.basename(value), open(value, 'rb'), 'application/octet-stream')
print(1)
else:
pass
multipart = MultipartEncoder(
@ -81,8 +81,18 @@ class apiSend(object):
header['Content-Type'] = multipart.content_type
response = requests.post(url=url, headers=header, timeout=timeout, data=multipart)
elif files is None:
multipart = MultipartEncoder(data)
response = requests.post(url=url, data=multipart, headers={'Content-Type': multipart.content_type},
try:
data_random = json.loads(data_random)
except:
pass
if not isinstance(data_random,dict):
data_random = valueHandle(str(data_random))
multipart = MultipartEncoder(
fields=data_random,
boundary='-----------------------------' + str(random.randint(int(1e28), int(1e29 - 1)))
)
header['Content-Type'] = multipart.content_type
response = requests.post(url=url, data=multipart, headers=header,
timeout=timeout)
else:
raise TypeError("files参数格式错误")