Base64 - Buffer - API Request

Does anyone have any experience taking a base64 string (I'm getting this by reading a file using an S3 resource block) and converting it to a Buffer or a blob that can be referenced inside in Raw JSON in an API Request block?

I understand this might be a niche workflow/use case but I'm having all kinds of issues passing the buffer values between a Javascript block (to convert the base64 to a buffer) and the API request.

Hey @pod2! Thanks for reaching out.

It definitely depends on the API that you're hitting, but I've generally had more success working with the base64 string instead of converting it. Here's one example of constructing a raw multipart payload, for example.

In general, though, the algorithm for converting a base64 string into a Blob should be similar to the below.

// Decode the Base64 string
    const binaryString = atob(base64);
    const len = binaryString.length;
    const bytes = new Uint8Array(len);

    // Convert binary string to byte array
    for (let i = 0; i < len; i++) {
        bytes[i] = binaryString.charCodeAt(i);
    }

    // Create a Blob from the byte array
    return new Blob([bytes], { MIME_TYPE });

Let me know if you have any follow up questions about that! I'm happy to take a look at your specific use case, as well. :+1:

For this particular use case we're querying a PDF from our S3 resource and trying to send it as an attachment using the Mailgun API.

During testing there was in fact a PDF attachment but it's corrupted and throws an error on open. My initial thought was it was double encoding hence the thought of providing something other than base64.

I'll try utilizing what you provided and see if that's helpful.

1 Like