How to upload multiple answers with image bytestring data?
Question: 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


Answer: 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

    
    
    img_data = base64.urlsafe_b64encode(raw_img)
    

**ETA:** In Python 3, the API expects the image data to be of type `str` so it
can be JSON serialized, but the `base64.urlsafe_b64encode` method returns the
data in the form of UTF-8 `bytes`. You can fix this by converting the bytes to
Unicode:

    
    
    img_data = base64.urlsafe_b64encode(raw_img)
    img_data = img_data.decode('utf-8')
    

