Skip to main content
Best answer

Converting an image to Base64

  • Jul 20, 2020 3:49 PM UTC
  • 5 replies
  • 4103 views

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?

Best answer by ikbelkirasan

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 }];

 

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

  • Author
  • Beginner
  • Jul 21, 2020 12:27 AM UTC

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'


ForYourIT
Forum|alt.badge.img+7
  • Tinkerer
  • Jul 21, 2020 10:08 AM UTC

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


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Solution Partner
  • Answer
  • Jul 23, 2020 9:19 PM UTC

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 }];

 


  • Author
  • Beginner
  • Jul 24, 2020 12:59 AM UTC

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


  • Author
  • Beginner
  • Jul 24, 2020 1:00 AM UTC

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!