According to Consumer Surveys docs, the questions[].images[].data
field takes a bytes datatype.
I'm using Python 3 for implementation, but the API is giving errors like Invalid ByteString
or bytes type is not JSON serializable.
I'm using the following code:
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
is part of the JSON payload that is being sent to the API.
Am I missing something? what's the correct way to handle image data upload for questions? I looked into https://github.com/google/consumer-surveys/tree/master/python/src
but there is not example of this part.
Thanks