我正在使用一个快速API来检索包含一些字节的mongo文档。结构如下
item =
{"namd" : "xyz",
"value1: b'\x89PNG\r\n\sla\..."
...
"some_other_byte: b'\x89PNG\r\n\sla\..."
}
使用FastAPI中的post请求返回上述数据,它尝试将其转换为json,但无法自动转换
所以我试了一下:
json_compatible_item_data = jsonable_encoder(item)
但是我得到了这个错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
有没有一种方法可以自动将上述dict转换为json,以便在restapi中返回?最好的方法是什么
使用FastAPI
jsonable_encoder
可以使用自定义编码器。将任意bytes
对象转换为base64str
的示例:在客户端对目标字段进行解码可以如下所示:
相关问题 更多 >
编程相关推荐