python - 使用请求使用 http post 将字节数组发送到 web 服务

标签 python json python-2.7 arrays python-requests

目前我正在尝试将字节数组发送到网络服务,但我收到错误消息,即字节数组不可序列化:

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

我正在使用下面的代码

发送请求

# 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)

设置数据方法:

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

我做错了什么,我必须使用其他方法来发送字节数组吗?

非常感谢大家!

最佳答案

如果你真的需要json,你必须对你的二进制数据进行编码。请参阅:Base64 encoding in Python 3

替代方案:How to send binary post data via HTTP?

关于python - 使用请求使用 http post 将字节数组发送到 web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35775615/

相关文章:

javascript - 在 View 中移动时, Angular 选择记住

python - 将数字数据分组到分箱范围内

C# - 尝试将字典转换为 JSON

python - 如何减少 Python selenium Pyperclip.paste 执行时间

python - 从 JSON 中过滤空值

python - django 从 1.3 升级到 1.9 生成 TypeError : Item in `` from list'' not a string

c - n 秒后终止 python 中的函数调用

带子循环和不带子循环的python列表理解

python - Elasticsearch python 库,如果存在则更新

python - 如何在 python 中使用 FFmpeg 和 subprocess 模块将 .mp4 视频文件转换为 .yuv (YUV420),反之亦然?