How to download images using Python in Zapier workflow

I’m working on a Zapier automation where I need to fetch an image using Python and then store it in Dropbox. I wrote some Python code but it’s throwing an error.

Here’s what I tried:

response = requests.get('https://picsum.photos/800/600')
image_content = response.content
return {'downloaded_image': image_content}

But I keep getting this error message: Runtime.MarshalError: Unable to marshal response: b'' is not JSON serializable

I think the issue is with how I’m handling the binary image data in the return statement. Has anyone successfully downloaded images in Zapier using Python code steps? What’s the correct way to handle image data so it can be passed to the next step in the workflow?

Any help would be great since I’m stuck on this part of my automation.

Yeah, I’ve run into this too. Zapier can’t handle raw binary data in returns. You need to encode it with base64 first, then decode it in the next step. Try import base64 then return {'image': base64.b64encode(image_content).decode()} - that should fix the marshal error.

The marshaling error occurs because Zapier requires data returned to be JSON-serializable. Directly returning binary image content will not work. I’ve faced this problem myself while creating an automation that took screenshots from webpages. The solution is to encode the binary data to base64 before returning it. Alternatively, you can save the image as a temporary file and return the file path, which is useful for workflows that involve file manipulations. Additionally, ensure the image URL returns proper image content instead of HTML error pages by checking the response status and headers.