Skip to main content
Best answer

Converting an image to Base64


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 ikbelkirasanBest 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
Did this topic help you find an answer to your question?
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
  • 3 replies
  • July 21, 2020

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
  • 259 replies
  • July 21, 2020

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 Expert
  • 555 replies
  • Answer
  • July 23, 2020

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
  • 3 replies
  • July 24, 2020
ForYourIT wrote:

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
  • 3 replies
  • July 24, 2020
ikbelkirasan wrote:

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!