我在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_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: Object of type BufferedReader is not JSON serializable
我怎样才能让它工作?
您可以将文件更改为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)
将字节转换为base64并在您的网站代码中解密