目录
本篇文章记录flask测试各种接口(与postman相对应)
本篇文章记录flask测试各种接口(与postman相对应)
最基本的:在Postman中利用body中的raw测试的:
json_url2=''
data2={
"username": "11111",
"password": "123456"
}
try:
response = requests.post(json_url2, json=data2)
print(response.json())
except requests.exceptions.RequestException as e:
print('请求出错:', e)
夹带token的:
json_url2=''
token = ''
data2={
"username": "111111",
"password": "123456"
}
try:
response = requests.post(json_url2, json=data2,headers=token)
print(response.json())
except requests.exceptions.RequestException as e:
print('请求出错:', e)
如果为data类型即用body中的formdata描述:
json_url2=''
data2={
"username": "11111",
"password": "123456"
}
try:
response = requests.post(json_url2, data=data2)
print(response.json())
except requests.exceptions.RequestException as e:
print('请求出错:', e)
加了file和data的
file = request.files['file']
# 如果用户没有选择文件
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
data1 = {
"stageId": ''
}
with open(file_path, 'rb') as f:
files = {
'file': (filename, f, 'image/png') # 根据实际文件类型设置MIME类型
}
try:
print(1)
response1 = requests.post(json_url1, data=data1, files=files, headers=headers1)
print(response1.json())
except requests.exceptions.RequestException as e:
print('请求出错:', e)
return jsonify({"message": "File successfully uploaded", "filename": filename}), 200
else:
return jsonify({"error": "File type not allowed"}), 400
配置环境(token)
在Headers中配置