使用requests库通过HTTP POST方法向webservice发送字节数组

6

目前我正在尝试将一个字节数组发送到一个Web服务,但是我收到了错误信息,指出一个字节数组不可序列化:

TypeError: bytearray(b'') 不可JSON序列化

我正在使用以下代码:

发送请求

# 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方法:

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

我做错了什么,我需要使用其他方法来发送字节数组吗? 非常感谢你们所有人!
1个回答

5

是的,我通常在C#中使用Newtonsoft json,它可以自动进行base64转换。谢谢,之前不知道这个! - BendEg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接