How to use form-data content type in Power Automate HTTP action

 Introduction to multipart/form-data in HTTP:

When working with HTTP requests, especially in scenarios involving file uploads or sending complex data structures, the multipart/form-data content type plays a crucial role. Unlike the traditional application/x-www-form-urlencoded content type, which is used for simple key-value pairs, multipart/form-data allows for the transmission of multiple parts of data, including files, in a single HTTP request.

In essence, multipart/form-data breaks down the data into distinct parts or fields, each with its own headers and content. This makes it ideal for scenarios where data needs to be structured in a hierarchical or multipart format, such as when uploading files and additional form fields.

In this tutorial, we'll delve deeper into understanding multipart/form-data in the context of HTTP requests, exploring its structure, use cases, and how to effectively utilize it in various scenarios, including within the context of Power Automate's HTTP action.

Multipart/form-data in HTTP Action: The API is called using the content type multipart/form-data. The multipart refers to the data divided into multiple parts and sent to the server. For each key-value pair part, you will have to construct something like

{ "headers": { "Content-Disposition": "form-data; name=\"KEY\"" }, "VALUE": "what ever value you would like to send" }

Backslash is used close the Content-Disposition header value else you will get Invalid-JSON.


The body of the HTTP action should have the two attributes $content-type and $multipart as shown below.

You can upload files using the form-data content type

 {

  "$content-type": "multipart/form-data",

  "$multipart": [

    {

      "headers": {

        "Content-Disposition": "form-data; name=\"CandidateName\""

      },

      "body": "Nikhil Mudgal"

    },

    {

      "headers": {

        "Content-Disposition": "form-data; name=\"file\"; filename=\"Abc.pdf\""

      },

      "body": {

        "$content-type": "application/pdf",

        "$content": File-Content

      }

    }

  ]

}

 

 

 



Comments