我有以下代码:
data = open('/tmp/books_read.png', "rb").read()
encoded = base64.b64encode(data)
retObj = {"groupedImage": encoded}
return func.HttpResponse(
json.dumps(retObj),
mimetype="application/json",
status_code=200)
。。。它抛出以下错误:
Object of type bytes is not JSON serializable Stack
我可以知道我该怎么解决这个问题吗
若它是您想要作为http响应发送的图像,那个么您不应该执行json.dumps,而是可以发送原始字节并接收它
但是,如果您仍然想这样做,则需要更改为
json.dumps(str(retObj))
base64.b64encode(data)
将以字节为单位输出对象encoded = base64.b64encode(data).decode()
将其转换为字符串之后,您可能需要(非常常见)对字符串进行url编码
相关问题 更多 >
编程相关推荐