post以base64上传图片使用data参数,直接上传图片使用file参数;但都不要自己给headers加Content_type参数
def test_uploadBase64(self):
“”"
base64上传文件
:return:
“”"
url = base_url +’/Upload/v1/SingleBase64’
file_path = r"C:\Users\fys\Desktop\image\4.png"
file_name = os.path.basename(file_path)
f = open(file_path, ‘rb’)
# 接口定义的上传字段为base64file, filename
data = {
"base64file": ‘data:image/png;base64,’+ base64.b64encode(open(file_path, ‘rb’).read()).decode(),
“fileName”: file_name
}
headers = {“Accept”: “application/json”}
headers[“token”] = globals()[‘token’]
res_base64 = requests.post(url, ‘POST’, data, headers)
print(‘res_base64’)
print(res_base64)
# 接口响应成功,存id ,预览图片时要使用
if res_base64[‘code’] == 200:
globals()[‘image_id’] = res_base64[‘data’][‘id’]
python3 requests上传base64图片
最新推荐文章于 2023-05-10 16:35:55 发布