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.

Lord Null
  • 856
  • 1
  • 9
  • 20

1 Answers1

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')) 
user2314737
  • 27,088
  • 20
  • 102
  • 114
  • I don't think that'll work on Zapier as you cannot import any libraries, like base64. (It has requests library by default.) – Lord Null Mar 10 '19 at 15:41
  • @LordNull the codecs library should be standard, does this work? – user2314737 Mar 10 '19 at 19:21
  • @LordNull Zapier does actually offer a few useful non-standard libraries. This includes base64. – Michael Case Mar 10 '19 at 23:01
  • 1
    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? – Lord Null Mar 11 '19 at 08:50
  • @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 – user2314737 Mar 11 '19 at 09:23
  • To build off of @user2314737 suggestion, it seems like you are already familiar with Python's request library. Why not leverage Dropbox's API and save yourself a zap step by completing it in the same code snippet. I would start by looking into Dropbox's [/upload](https://www.dropbox.com/developers/documentation/http/documentation#files-upload) endpoint. – Michael Case Mar 12 '19 at 00:26