Best answer

Converting an image to Base64

  • 20 July 2020
  • 5 replies
  • 3018 views

Userlevel 1

I’m trying to pass an image from a URL to a POST Webhook. The API endpoint will only accept an image payload in Base64. Looking to use Code by Zapier to accomplish the translation, but I’m coming up short. Has anyone managed to devise a similar solution?

icon

Best answer by ikbelkirasan 23 July 2020, 23:19

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

5 replies

Userlevel 7
Badge +12

Hi @simpledcp - I’d suggest using the following JavaScript code snippet:

const response = await fetch(inputData.url);
const content = await response.buffer();
const data = content.toString("base64");
output = [{ data }];

 

Userlevel 1

Hi @simpledcp - I’d suggest using the following JavaScript code snippet:

 const response = await fetch(inputData.url);
const content = await response.buffer();
const data = content.toString("base64");
output = [{ data }];

 

That did the trick! Now I’m just running up against the limitations of the Zapier code function. Most of my images are a wee too big. Thanks!

Userlevel 7
Badge +7

Hi @simpledcp ,

Unfortunately this is not possible with the Code by Zapier step using python. According to this page it doesn’t support any importing and only supports a few libraries.

Another solution would be to use an online API to do this conversion for you. I haven’t been able to find one myself but maybe you succeed. 

Let me know if this helped you!

~Bjorn

Userlevel 1

Hi @simpledcp ,

Unfortunately this is not possible with the Code by Zapier step using python. According to this page it doesn’t support any importing and only supports a few libraries.

Another solution would be to use an online API to do this conversion for you. I haven’t been able to find one myself but maybe you succeed. 

Let me know if this helped you!

~Bjorn

 

Well, that’s that’s a pretty rock solid reason why I can’t do this in Python :-P

Userlevel 1

Using the following python:

 

import base64
import requests

response = requests.get(input_data['url'])
str = base64.b64encode(response.content).decode('utf-8')
return str

 

but I keep getting 

'str' object has no attribute 'copy'