2

I am trying to use a Python code step to download an image in Zapier. Here is some sample code (but it doesn't seem to work):

r = requests.get('https://dummyimage.com/600x400/000/fff')
img = r.raw.read()
return {'image_data': img}

The error I get is Runtime.MarshalError: Unable to marshal response: b'' is not JSON serializable

Does anyone know how I can use requests in a Python code step in Zapier to get an image? (I am trying to get the image and save it to Dropbox.) THANKS.

2
  • How does this have to do with Zapier at all? Commented Mar 10, 2019 at 13:21
  • See here zapier.com/help/code-python Commented Mar 10, 2019 at 13:33

1 Answer 1

2

It looks like you need a json serializable object and not a binary object. One way to convert your image to a string is to use base64 and then encode it:

Make the image serializable:

r = requests.get('https://dummyimage.com/600x400/000/fff') 
img_serializable = base64.b64encode(r.content).decode('utf-8')                                                                         
# check with json.dumps(img_serializable)

Now return {'image_data': img_serializable} should not give errors.

Recover image from string and save to file:

with open("imageToSave.png", "wb") as f: 
    f.write(base64.decodebytes(img_serializable.encode('utf-8'))) 

The same using codecs, that is part of the standard Python library:

r = requests.get('https://dummyimage.com/600x400/000/fff') 
content = codecs.encode(r.content, encoding='base64') 
img_serializable = codecs.decode(content,encoding='utf-8')                                         

type(img_serializable)                                                                                                                 
# Out:
# str

with open("imageToSave3.png", "wb") as f: 
    f.write(codecs.decode(codecs.encode(img_serializable, encoding='utf-8'), \ 
                            encoding='base64')) 
Sign up to request clarification or add additional context in comments.

6 Comments

I don't think that'll work on Zapier as you cannot import any libraries, like base64. (It has requests library by default.)
@LordNull the codecs library should be standard, does this work?
@LordNull Zapier does actually offer a few useful non-standard libraries. This includes base64.
Ah yes, I see that that now, it says standard Python library is available. I tried the examples above but they are not quite working for me. The image is saved as base64 data to the output (return {'image_data': img_serializable}) but in the next action step it is being saved to Dropbox as a text file. How do I make the action step understand that it is an image, so that an image is saved to Dropbox and not just a text file containing base64 text?
@LordNull maybe you should post your code for uploading to Dropbox in another question. Also, I don't know Zapier but I wonder if one can stream the file directly to Dropbox without saving it on the server first
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.