-3

I'm sending form data and images in a POST request to my backend:

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)

The above throws python : TypeError: Object of type BufferedReader is not JSON serializable

How can I make it work ?

mark
  • 1
  • 1
  • 1
  • why are you trying to assign an entire file (returned from `open`) to a json key? Consider only sending the path to the file instead, the frontend should be able to serve it – Chase Dec 11 '20 at 20:09
  • Does [this](https://stackoverflow.com/questions/22567306/python-requests-file-upload) answer your question? – sarartur Dec 11 '20 at 20:19

3 Answers3

-1

You can change your file to base64

heydar dasoomi
  • 527
  • 1
  • 4
  • 19
-1

I went with:

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)
mark
  • 1
  • 1
  • 1
-1

convert bytes into base64 and decrypt it in your website code

Nighty
  • 1