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
You need to use web-safe/URL-safe encoding. Here's some documentation on doing this in Python: https://pymotw.com/2/base64/#url-safe-variations
In your case, this would look like
ETA: In Python 3, the API expects the image data to be of type
strso it can be JSON serialized, but thebase64.urlsafe_b64encodemethod returns the data in the form of UTF-8bytes. You can fix this by converting the bytes to Unicode: