Rest API
HTTP Connector Power Automate
Manish Solanki  

How to post multipart/form-data using http connector in Power Automate?

Use Case

Last week, I spent a lot of efforts in calling HTTP request from external or 3rd party system. While working on it I face challenges with passing the file content. There is not much documentation available on handling this scenario so I thought of documenting the steps to pass form/data with file in http request.

Scenario

We need to call a rest endpoint that accepts multipart/form-data. Basically, it is an email with some text fields like subject, body etc. along with an attachment. The purpose of sending the attachment of file is to extract the data from file content by external system that internally uses AI model.

Solution

1.Add “Get file content” to get the content of file that needs to send as an attachment. Please make sure that you set the ‘Infer Content Type’ parameter to ‘Yes’. In this example, I have hard coded the file path but you could pass it dynamically as per the need:

2. Next, add “Compose” that will hold the double quotation which will be needed when we create the request body in http action. I have taken this step as I have observed that sometimes http request fails because of incorrect JSON. So, added this step to avoid this scenarios:

Input value in compose action:

"

3. Finally, add “Http” action to call the rest endpoint. Set the Method & URI parameter. In the body, we will create a JSON code.

Each parameter has 2 parts: header & body. The header contains the name of the parameter and body contains the value of that parameter.

In case of file parameter, we need to additionally pass the file name.

I have used rand function for the ID to get random number and set the file name as current utc time stamp using utcNow function. But you can modify all those parameters as per the requirements.

The JSON code in the request body looks like:

{
  "$content-type": "multipart/form-data",
  "$multipart": [
    {
      "headers": {
        "Content-Disposition": "form-data; name=@{outputs('Compose')}uniqueIncEmailId@{outputs('Compose')}"
      },
      "body": "@{rand(1,1000)}"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=@{outputs('Compose')}emailSubject@{outputs('Compose')}"
      },
      "body": "test subject"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=@{outputs('Compose')}emailBody@{outputs('Compose')}"
      },
      "body": "test email body"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=@{outputs('Compose')}tokenId@{outputs('Compose')}"
      },
      "body": "xxxxx-token id-xxxxx"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=@{outputs('Compose')}Attachment1@{outputs('Compose')}; filename=@{utcNow('yyyyMMddHHmmss')}.pdf@{outputs('Compose')}"
      },
      "body": @{body('Get_file_content')}
    }
  ]
}

Please note that you should place your file in SharePoint library or one drive before passing the content in the body of the parameter. As when we get the file from SharePoint or one drive, we would get the content type along with content bytes.

Output

Output of Http action:

Conclusion

Through this example, we have seen the technique of passing the header & body of each parameter when calling multipart form/data request.

Leave A Comment