Python 如何排除故障;BufferedReader类型的对象不可JSON序列化;?
我正在向我的后端发送POST请求中的表单数据和图像:Python 如何排除故障;BufferedReader类型的对象不可JSON序列化;?,python,json,python-requests,Python,Json,Python Requests,我正在向我的后端发送POST请求中的表单数据和图像: data = { 'username': self.ids['username'].text, 'email': self.email_field.text, 'password': self.ids['password'].text, 'data': json.dumps({ 'first_photo': open(f'{self.selfies_pat
data = {
'username': self.ids['username'].text,
'email': self.email_field.text,
'password': self.ids['password'].text,
'data': json.dumps({
'first_photo': open(f'{self.selfies_path}/first.png', 'rb'),
'second_photo': open(f'{self.selfies_path}/second.png', 'rb'),
'third_photo': open(f'{self.selfies_path}/third.png', 'rb')
})
}
response = requests.post('http://5.10.203.170/auth/register', files=data)
上面抛出的python:TypeError:BufferedReader类型的对象不可JSON序列化
如何使其工作?您可以将文件更改为base64我使用了:
data = {
'first_photo': open(f'{self.selfies_path}/first.png', 'rb'),
'second_photo': open(f'{self.selfies_path}/second.png', 'rb'),
'third_photo': open(f'{self.selfies_path}/third.png', 'rb'),
'data': json.dumps({
'username': self.ids['username'].text,
'email': self.email_field.text,
'password': self.ids['password'].text
})
}
response = requests.post('http://5.10.203.170/auth/register', files=data)
为什么要尝试将整个文件(从
open
返回)分配给json密钥?只考虑向文件发送路径,前端应该能够服务它回答你的问题吗?