The more time I spend with Logic Apps, the more I love the functionality they can bring to the table, the ease at which you can automate a business flow with limited coding experience is fantastic. I wanted to share some experience of working with blob storage in Logic Apps and matching content type when the content type is unknown.
I have been working recently on Logic Apps which retrieve emails matching a specific criteria from a mailbox, uses regular expressions on known subjects to chunk up the data, output it to JSON which processes to a database. It also stores the attachments in blob storage.
The issue I had is that when you create a blob using the connector, it's not immediately obvious how to set the content type, the outputs from the email trigger in Office 365 does indeed contain the content type of the attachment. It turns out that on the Create blob action in Logic Apps, you can pass a JSON payload which provides not only the content of the attachment but also the content type, this is exactly what I was looking for.
The first step is to use a Compose action, in here you can add a JSON string which allows you to define both the content and content type together. You can see it's a fairly standard format, I've added in references to Attachments Content and Attachments Content-Type which comes directly from the Attachments loop in the Office 365 connector. If you look at the job execution for this step, you will see exactly what is happening.
You can see now that both the $content-type and $content properties of the JSON object are now filled in with the contents of the data passed. You can see that the content bytes are shown here and the content type is correctly set to application/pdf. This means when looking at the PDF it will now render appropriately.
Back in the designer the final step is the create the blob. Due to the work we have done using the compose action, this is now really straight forward.
As you can see above, we just reference the Outputs property from the previous Compose action. This passes the JSON created to the blob content parameter and instructs the blob to be created with the content type property set. The result of which, if we look in the container and look at the blob properties, you can see the content type is correctly set.
There you have it, working with content types when it's not obvious can seem tricky. This is actually documented in the official documentation. A number of casts and functions exist for working with most common content types when it comes to working with APIs such as JSON and XML, however this enables you to work with other content types.