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

12,858

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?

Share:
12,858
BendEg
Author by

BendEg

Dlr/IronPython projects Simplic-Dlr SOreadytohelp

Updated on June 29, 2022

Comments

  • BendEg
    BendEg almost 2 years

    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!