How to use form-data and form-urlencoded content type in Power Automate or Logic Apps HTTP action

Content type multipart/form-data is used to send both text and binary data to the server and x-www-form-urlencoded is used more generally used to send text data in a query string in the form of name value pairs separated by ampersand. In this blog post, let us see how to use the content-type

  • multipart/form-data
  • x-www-form-urlencoded

in a Power Automate or Logic apps HTTP action to post data with an API which has implemented the content-type. Find below the screenshot from postman with a sample API

multipart/form-data in HTTP Action:

From the above screenshot, the API is called using the content type multipart/form-data. The multipart refers to the data (in the above screenshot it is To, From & Body) which is divided into multiple parts and sent to server. For each key value pair aka 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.

To call the API displayed from the above screenshot on the HTTP Action, the body of the HTTP action should have the two attributes $content-type and $multipart as shown below

{
  "$content-type": "multipart/form-data",
  "$multipart": [
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"To\""
      },
      "body": "whatsapp:+123456"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"From\""
      },
      "body": "whatsapp:+178910"
    },
    {
      "headers": {
        "Content-Disposition": "form-data; name=\"Body\""
      },
      "body": "Your appointment is coming up on July 21 at 4PM"
    }
  ]
}

You can upload files using the form-data content type

{
      "headers": {
        "Content-Disposition": "form-data; name=\"file\"; filename=\"fileName.png\""
      },
      "body": "file-content"
}

The file content can be the output of the SharePoint or OneDrive connector.

x-www-form-urlencoded in HTTP Action:

The x-www-form-urlencoded content type has its form data which is encoded and sent in a single block on the HTTP request body. To call the sample API from the screenshot posted at the top of this post in the HTTP Action, the form values must be encoded & the values be separated by ampersand. Expression encodeUriComponent can be used to encode the form values

Headers:

Key: Content-Type

Value: application/x-www-form-urlencoded

Body (Separated by &):

Key=Value&Key=Value

Find below screenshot for your reference

Hope you have found this informational & thanks for reading. If you are visiting my blog for the first time, please do look at my other blogposts.

Do you like this article?

Subscribe to my blog with your email address using the widget on the right side or on the bottom of this page to have new articles sent directly to your inbox the moment I publish them.

5 thoughts on “How to use form-data and form-urlencoded content type in Power Automate or Logic Apps HTTP action

  1. Thank you, this has helped me a lot. Have you tried setting this up via a logic apps custom connector? I can get it working via a direct http call, but not via a custom connector…

    Like

  2. When I use this JSON:
    {
    “headers”: {
    “Content-Disposition”: “form-data; name=\”file\”; filename=\”fileName.png\””
    },
    “body”: “file-content”
    }

    The system does not recognise it as a png or pdf, just plain text. How can I specify it?

    Thanks!!

    Like

  3. I got so close to a similar problem by using this advice.

    In my case, this line wasn’t quite accurate:
    “body”: “file-content”

    The right syntax was “body”: {file-content variable with no quotes}

    Also, I was trying to solve using Power Automate for the Power BI API to Publish a file. Here’s the full Body JSON:

    {
    “$content-type”: “multipart/form-data”,
    “$multipart”: [
    {
    “headers”: {
    “Content-Disposition”: “form-data; name=\”TestAPI\”; filename=\”TestAPI.pbix\””
    },
    “body”: @{body(‘Get_pbix_File_Content’)}
    }
    ]
    }

    Liked by 1 person

Leave a comment