Introduction
Microsoft has provided tons of connectors in FLOW to interact with hundreds of applications out there. Still, at some point, you might need to build your own connector or build REST APIs which are communicating to your other applications. You call your REST APIs from FLOW easily with HTTP action. This is a powerful action which you can use to pull data, push data, update data. In this article, we are going to look at how we can send JSON data as a body to HTTP action correctly.
Problem
When it’s data then, of course, it must be generated dynamically depending upon your source request item. In FLOW you can generate JSON data by contenting the values and storing them in a string variable. But when you pass this string variable to HTTP action it fails. It treats data as a plain text body parameter, not JSON. You will get an UnsupportedMediaType error as shown below.
The request entity's media type 'text/plain' is not supported for this resource.
Solution
Let’s see how to overcome the above error. We need to parse the string into JSON object and then send it to HTTP action.
Let’s see how to do it step by step.
Login to Microsoft FLOW web service and Edit your FLOW >> Add a variable in which you can store the concatenated string JSON data >> Set the variable with your dynamic content in JSON format.
When I say JSON format, I mean a string like {"Parameter1": "Value1", "Parameter2": "Value2"}
Add Parse JSON action after it >> Set the Content of this action to your variable, i.e., strJsonData in our case. Now, we need to tell this action how the schema of our JSON data is. So, click on "Use sample payload to generate schema" – this will help to generate the schema automatically instead of typing it manually and making errors.
Enter sample JSON data which your REST API function accepts, as shown below >> click on "Done".
- {
- "CaseID": "CID-1234",
- "CaseName": "Desk rearrangement"
- }
That’s how the schema will get generated for your JSON input. Depending on the sample data values, it will see the properties and type. You can change this schema any time if its not supporting to your API. Sometimes, the type might be empty – in that case, you need to explicitly specify it as string.
Now, add HTTP action, which we will use to call our REST API and send the above JSON data to it.
Enter your API URL, choose Method POST.
In the Body section, choose Body from Parse JSON action as shown below.
If you want to know how to manage authentication in HTTP action, then, please read
this article which explains how to call authenticated APIs from FLOW.
We are done here. That’s how all the actions in sequence will look like in the FLOW.
It is time to test this FLOW. Click on Test and it should run without any error now. The HTTP action will recognize the JSON data correctly this time.
To summarize, you need to use the "Parse JSON" action in order to generate the JSON data object from a string and send it to HTTP action.
That’s it for this article. Hope this helps. Thanks for reading.