6

Currently I'm trying to send a byte-array to a webservice, but i get the error message, that a bytearray is not serializeable:

TypeError: bytearray(b'') is not JSON serializable

I'm using the following code

Sending the requests

# Set blob
with open('demo-file.txt') as file:
    f = file.read()
    b = bytearray(f)
    print a.set_data('5cb9bc4d-c0fd-40ab-8b74-4e62b50d8966', b)

Set_Data method:

def set_data(self, path, data):
    """
    Save data in

    Parameter
    --------
    path (str): Path as string
    data (bytearray): Data as bytearray
    """

    result = requests.post(self.url + '/set', json = { 'path': path, 'data': data})

    # Check status and token
    if result.status_code == 200:
        return result.text

What am I doing wrong, do I have to use some other methods for sending bytearrays?

Thank you all a lot!

BendEg
  • 20,098
  • 17
  • 57
  • 131

1 Answers1

5

If you really need json, you have to encode your binary data. See: Base64 encoding in Python 3

An alternative: How to send binary post data via HTTP?

Community
  • 1
  • 1
Meiko Rachimow
  • 4,664
  • 2
  • 25
  • 43
  • Yep, I normally use Newtonsoft json in c# and that make the base64 conversion automatically. Thank you, did not know that before! – BendEg Mar 03 '16 at 15:15