根据消费者调查文档,该questions[].images[].data
字段采用字节数据类型。
我正在使用 Python 3 来实现,但是 API 给出了类似Invalid ByteString
或字节类型的错误is not JSON serializable.
我正在使用以下代码:
import base64
import urllib
url = 'http://example.com/image.png'
raw_img = urllib.request.urlopen(url).read()
# is not JSON serializable due to json serializer not being able to serialize raw bytes
img_data = raw_img
# next errors: Invalid ByteString, when tried with base64 encoding as followings:
img_data = base64.b64encode(raw_img)
# Also tried decoding it to UTF.8 `.decode('utf-8')`
img_data
是发送到 API 的 JSON 有效负载的一部分。
我错过了什么吗?处理问题图像数据上传的正确方法是什么?我调查过,https://github.com/google/consumer-surveys/tree/master/python/src
但没有这部分的例子。
谢谢