Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using requests to send byte-array to webservice using http post

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!

like image 391
BendEg Avatar asked Sep 25 '22 04:09

BendEg


1 Answers

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?

like image 189
Meiko Rachimow Avatar answered Sep 28 '22 06:09

Meiko Rachimow