Using Multipart/Form-Data in a Power Automate or Logic Apps HTTP Action

In HTTP terms, Content-Type ‘header’ is used by the client application when it submits data to the server. 

That being said, one of the most used Content-Types is ‘Multipart/Form-Data‘. In a ‘Multipart’ HTTP request, each field to be sent has it’s own Content-Type, file name, and data separated by a boundary from the other field(s).

With ‘Multipart/Form-Data’ the characters are NOT encoded. This is important to know when you send the file binary data to the Server, since this ensures the ‘Multipart/Form-Data’ in the bitstream is not altered.

One other important thing to note is that ‘Multipart/Form-Data’ is quite complicated/useful, as it allows entire files to be included in the data.

In this post, I’ll explain how a Citizen developer can use ‘Multipart/Form-Data‘ in a Power Automate or Logic Apps HTTP action.

UseCase \ Scenario –

We will be invoking the PSPDFKit “PDF to Image API” and Convert PDF files to PNG, JPG, and WebP.

Before we begin, please make sure the following prerequisites are in place –

  • Power Automate subscription.
  • PSPDFKit API subscription, full or trial subscription (Start trial).
  • Download and Install Postman.
  • Working knowledge about Web APIs, Power Automate, and Postman.

Before we start with authoring a request in Power Automate, I would highly recommend you first test the API Request in Postman. To test the PSPDFKit “PDF to Image API” you can use the official Postman collection.

Pro Tip to understand how the request body in Postman, Click on the “Code” link under the “Save” button and select “HTTP“.

In the above code, you can see that the fields “instructions‘ and ‘document‘ have their own content types. This is the basic information you’ll need to Author a request in Power Automate.

YourMultipart/Form-Data’ body will look like the sample below –

{
  "$multipart": [
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"instructions\""
      },
      "body": "{\r\n      \"parts\": [\r\n        {\r\n          \"file\": \"document\"\r\n        }\r\n      ],\r\n      \"output\": {\r\n        \"type\": \"image\",\r\n        \"format\": \"png\",\r\n        \"dpi\": 300\r\n      }\r\n    }"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"document\"; filename=\"document\""
      },
      "body": @{body('Get_file_content')?['body']?['$content']}
    }
  ],
  "$content-type": "multipart/form-data"
}

Pro Tip – Use an Online JSON Escaper to construct your JSON body.

Setting up the Power Automate(Flow)

From a high level, our Flow looks as follows:

Step 1: For this demo, we’ll use “For a selected file” SharePoint Trigger action and “Add an input” of type “Email”. We use this trigger to start the Flow from the SharePoint Online User Interface.

Step 2: Add the “Get file properties” action and configure it with reference to the details below:

  • Site Address : Specify the path to the SharePoint Online site-collection which holds the file.
  • Library Name :  Select the Library Name from the dropdown
  • Id : Output of the ‘For a selected file‘ action.

Step 3: Add the ‘Get file content‘ SharePoint action and pass the ‘Identifier‘  (Output of ‘Get files properties‘ action) into the ‘File Identifier‘.

Step 4: Add the HTTP action.

Headers:

  • Authorization: <your_bearer_token>
  • Accept: */*

Body:

{
  "$multipart": [
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"instructions\""
      },
      "body": "{\r\n      \"parts\": [\r\n        {\r\n          \"file\": \"document\"\r\n        }\r\n      ],\r\n      \"output\": {\r\n        \"type\": \"image\",\r\n        \"format\": \"png\",\r\n        \"dpi\": 300\r\n      }\r\n    }"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"document\"; filename=\"document\""
      },
      "body": @{base64ToBinary(outputs('Get_file_content')?['body']?['$content'])}
    }
  ],
  "$content-type": "multipart/form-data"
}

Note: You need to pass the “File Content” to the HTTP body as Binary.

Step 15: Add a ‘Create File‘ action.

  • Site Address: Set to the target SharePoint site.
  • Folder Path: Set to the target SharePoint library and folder.
  • File Name: Give a meaningful ‘File Name‘ to the created PDF document.
  • Note: Please make the file name unique, or multiple runs of the flow will overwrite the same file.  I recommend basing it on the source file name, but with some kind of suffix.
  • File Content: Select the ‘Body‘ field from the ‘HTTP‘ action.

All Done! Navigate back to the original library and select the PDF document you want to Convert to PNG. Click on the ‘Button‘. It should open the blade and click on ‘Run Flow‘. After a few seconds, you should have a PNG in the destination library.

Subscribe to this blog for the latest updates about SharePoint Online, Nintex, Microsoft Flow, Power Apps, and document conversion and manipulation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s