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!

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, I normally use Newtonsoft json in c# and that make the base64 conversion automatically. Thank you, did not know that before!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.