应用场景:用postman、apifox测试时请求python接口,python接口接收上送过来文件对象
下面截图是apifox上送的文件对象
python接口中,接收此文件对象
# -*- coding:utf-8 -*-
from flask import Flask, request, json
import os
from Config.constant import save_base_path, date_format, min_random, max_random,\
count_random, file_split1, file_split2
def get_file_content(file_path):
""" 读取图片 """
with open(file_path, 'rb') as fp:
return fp.read()
@app.route('/test', methods=['POST'])
def test():
request_sn = request.form['request_sn']
attr = request.form['attr']
img_path_file = request.files.get('image') # FileStorage
# 图片保存至指定路径
i_path = save_base_path + '/' + img_path_file.filename
print(i_path)
img_path_file.save(i_path)
# 读取图片bytes
image = get_file_content(i_path)
print(type(image))
print(image)
打印
/home/dev/datafiles/test/mao.jpeg
<class ‘bytes’>
b’\xb5\x10iqi\x10\xdd\xde\x82X6\xc7\xb5Q\xaa\xcc\xea\xb7\x90\x7f\xdd$\xea\x9d\xb0\x9f\x95L\xdcz\xa4-\xce\xdcPfn\xcd\xa4\x92\xcf\x97’