Friday Fact: Logic Apps decode all sorts of Base64 files, but, sometimes, not the way you expect!

When you need to decode a Base64 file, you can rely on Logic Apps to do it, in concrete you can lean on an expression to help you achieve that. To put it into context, let’s say you have a Logic App with the When a HTTP request is received trigger, and you receive a Base64 encoded PDF.

In this Friday Fact we will use Postman to send that encoded PDF into the Logic App.

Naturally, what first occurs to us is to decode the Base64 content, and to do so, we will use a Compose and the expression decodeBase64.

The expression:

decodeBase64(string(triggerBody()['body']))

In truthfulness here lies the problem, because further ahead you want to send that decoded Base64 file via email, and using the expression decodeBase64 will not work properly.

So, how can we decode the Base64 file into a PDF?

We simply need to use another expression instead of the decodeBase64, and that expression is:

  • base64ToBinary

But what is so different about these expressions:

  • decodeBase64: This function takes a Base64 encoded string as input and decodes it into its original binary form. It’s like taking a coded message and decoding it into its original content. So, if you have a Base64 encoded string, you’d use decodeBase64 to turn it back into its binary representation.
  • base64ToBinary: This function is used to convert a Base64 encoded string directly into a binary format. It doesn’t decode the string like decodeBase64. Instead, it just converts it into binary without decoding it. Think of it like changing the format of a file without understanding its content.

But there is the concept and the reality of it. We might think that converting that string to Base64 will be enough to convert the PDF or any other file, but that is not right.

If we do it that way, what will happen is that we will have a corrupted file that will not open or be recognized or a blank PDF without content, for example:

So, what I recommend is to use the base64ToBinary expression.

The expression:

base64ToBinary(triggerBody()['body'])

This way, you will be sure that the content you receive will be correctly decoded into the original content, making this process less prone to errors and making life easier!

And remember, this approach works on all sorts of files, from PDFs to PNGs, JPGs, or Text Documents…

To lazy to read? We’ve got you covered! Check out our video version of this content!

Join us next Friday for another Friday Fact!

Hope you find this helpful! If you enjoyed the content or found it useful and wish to support our efforts to create more, you can contribute towards purchasing a Star Wars Lego for Sandro’s son!

Author: Luis Rigueira

Luis Rigueira is a Enterprise Integration Consultant at DevScope

Leave a Reply

Your email address will not be published. Required fields are marked *

turbo360

Back to Top