小编Mou*_*dri的帖子

使用python将json和文件发送到flask

我有这个问题,我试图在单个函数中向烧瓶 API 发送/接收一些文件和 JSON。

在我的客户(发件人)上,我有:

#my json to be sent 
datas = {'var1' : 'var1','var2'  : 'var2',}
#my file to be sent 
local_file_to_send = 'user_picture.jpg'
url = "http://10.100.2.6:80/customerupdate"
headers = {'Content-type': 'multipart/form-data'}
files = {'document': open(local_file_to_send, 'rb')}
r = requests.post(url, files=files, data=datas, headers=headers)
Run Code Online (Sandbox Code Playgroud)

在我的 Flask 服务器上,我有:

class OPERATIONS(Resource):
        @app.route('/',methods=['GET'])
        def hello_world():
            return 'Hello World!'

        @app.route('/customerupdate',methods=['GET','POST'])
        def customerupdate():
            event_data_2 = json.loads(request.get_data().decode('utf-8'))
            print event_data_2
Run Code Online (Sandbox Code Playgroud)

我收到这条错误消息,告诉我数据实际上既不是 json 格式也不是 utf8 格式。如果我打印“get_data”的内容而不尝试解码它会显示一些二进制字符..

我的客户端读取 json 并在本地写入文件的语法是什么?

post json file request flask

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

file ×1

flask ×1

json ×1

post ×1

request ×1