If you have ever worked with multipart bodies, you know it’s basically just multiple types of content enclosed within boundaries. It seems like plain text, so one may assume it’s as simple as adding text in an Azure Logic App, right? Wrong! Creating a multipart body using plain text does not provide a working solution, unfortunately.
Azure Logic Apps’ native language is JSON, which means that creating a multipart body also is constructed in JSON. This blog post guides you step-by-step through the process on how to set this up. I’ve created an Azure Logic App to illustrate the solution, but feel free to be creative and come up with your own solutions!
Normally, when you want to create a multipart body, you just start creating the boundaries, adding content in between and you’re good to go. However, with Azure Logic Apps having JSON as a natural language, the example below does not work:
-----------------------------saas
Content-Type: application/json
{
"message": {
"text": "This is a test",
"title": "test"
}
}
-----------------------------saas
Content-Disposition: form-data; name="file"; filename="file.txt",
Content-Transfer-Encoding: base64,
Content-Type: text/plain
dGVzdA==
-----------------------------saas--
At least, not in plain text as noted above. Not even when you explicitly set the content type to “multipart/form-data” for the receiver . It is still possible though to create the output like above, only you have to make sure to instruct the Azure Logic App to do so by applying a special JSON syntax. I’ve created an example for this blog post, consisting out of a couple of steps:
Prerequisites
When following the steps in this blog post I assume the following:
- You’re familiar with sending multipart bodies, and what it beholds
- Azure Logic Apps itself are no secret to you
- Encoding and body types rings a bell
Create array variable
The first thing I did was adding a variable named “multipartBody” which will hold all parts of the multipart body. I find it easy to do this using a variable as I can reference it easily in the envelope we’re creating later on, but off course there are more ways to achieve the same (such as a compose action with a ‘For Each’ action or a ‘Select’ action).
Fill array with contents
Next we’re going to fill the array with our first part of the multipart body, in this example a JSON-body. The JSON contains a message object with a title and a text.
The JSON is added within a “body” object. This is where you normally add the contents of your body in a multipart setup. Also a “headers” object is added, containing all headers necessary for this particular body (in this example it’s instructing the receiver that this part is a JSON content type). These headers are normally added directly below the boundary.
I’ve added the code above for your reference below:
{
"body": {
"message": {
"text": "This is a test",
"title": "test"
}
},
"headers": {
"Content-Type": "application/json"
}
}
Okay, so we’ve managed to add our first part! Now we’re going to add another one, otherwise it wouldn’t be a multipart off course 😉 It is a textfile containing the word “test”, however this time the contents are encoded as BASE64.
Using the “headers” object we’re adding another header to make sure the receiver knows it’s encoded as such. Also we’re adding a header to communicate on the file name (which obviously comes in handy for a file).
For your reference also the code from the example above is added here:
{
"body": "dGVzdA==",
"headers": {
"Content-Disposition": "form-data; name=\"file\"; filename=\"file.txt\"",
"Content-Transfer-Encoding": "base64",
"Content-Type": "text/plain"
}
}
Create envelope
Now we’ve added our parts to the array it’s time to form the envelope instructing the Azure Logic App to construct a multipart body. This is where the magic happens as this syntax makes sure the Azure Logic App knows what to do with the body when sending it.
To achieve this I’ve created a ‘Compose’ action containing a JSON object with a “$multipart” and “$content-type” attribute. The last one also containing the boundary (“saas” in my example, feel free to change it to your requirements).
And off course, the example above can be found as code again below:
{
"$content-type": "multipart/form-data; boundary=saas",
"$multipart": @{variables('multipartBody')}
}
Call endpoint
Creating the multipart JSON itself is not the only thing necessary. It’s just one part of the trick.
When calling the actual endpoint, where we add the outputs of our ‘Compose’ acion as ‘Body’, we also need to provide a “Content-Type” header with the value “multipart/form-data”. With the combination of this header, along with the body constructed in this example, the Azure Logic App knows what to do.
And there you go! We’ve successfully sent a multipart body through Azure Logic Apps!
Summary
We’ve created an array variable and filled it with all parts of the multipart body, making sure we’ve added all necessary attributes for the multipart body to be handled correctly. Next we’ve added an envelope around the array variable to instruct the Azure Logic App that this is a multipart body. Next, we’ve topped it off by telling the receiver what to expect using the “content-type” header.
What I did not touch in this blog post is all possible logic to retrieve and add your actual contents, which I leave to your imagination. Still I hope this blog inspired you to build your own solutions. Feel free to drop me a note in the comments below! I’m happy to think along with you 🙂