I recently received some Logic Apps questions, each of which I would like to address within a series of blog posts. This post covers how to construct a simple Http POST request. In addition, I also explain how to configure basic authentication.
Logic Apps post form data
Creating a Logic Apps workflow step responsible for sends a POST request is very simple however, the interface might be somewhat confusing the first time.
To make sure all is easy to understand, I’m creating a small Logic App which is responsible for submitting Post data based on a set interval. For this example, I’m posting data to GTmetrix, a speed a service which analyzes webpage performance. Given that GTmetrix required HTTP Basic Access Authentication, we need to make sure to provide authentication headers as well.
Preparing and testing a request.
Before we can start creating the Logic Apps workflow, we first need to construct the HEADERS and BODY data. The easiest way to do this is by using Postman.
Step 1 – Open postman to construct and test your HTTP post request. Set the method to POST and provide the Url.
Step 2 – Locate the Authorization tab and provide your credentials. After completion, postman will create the authorization header. This step is only required if the destination requires any form of authentication.
Step 3 – To construct a body message, open the Body tab. Select form-data and enter your form data in key-value pairs.
At this point, it’s time to test and tweak your request. Debugging within Postman is a lot faster and therefore recommend constructing requests within Postman first.
After all is working within Postman, we can generate a raw message, allowing us to copy the generated HEADERS and BODY data.
Note: Logic apps are constructed using JSON and therefore it’s required to properly escape the output. Fortunately, postman can do this for us when using the Generate code option.
Step 4 –Locate the Generate Code button located in the upper right part of the interface. A new window will open. Change the default HTTP output to JAVA OK HTTP. This is needed to make sure the message is escaped properly. Locate and copy the values for mediaType, content-type, and authorization.
Creating the Logic App workflow
Step 1 – Start by creating a new workflow and add a new Recurrence logic block.
Step 2 – Add a new follow-up cation of type HTTP. I’ve renamed the HTTP block New Test Request. This way, it’s easy to understand what each block is doing.
Step 3 – Expend the HTTP block we’ve just added and set the METHOD to POST.
Step 4 – Open the Logic App in code view. Within the HTTP action, add the following code; however, replace my values with the values gathered from Postman.
The Body should contain the value from mediaType, Authorization the same as Authorization, and Content-Type the value from content-type.
At his point, you can test your workflow by starting the workflow manually using Run Trigger.
Once completed, you can add additional steps for handling the response message. I’ve already written several posts covering how to parse JSON and text data which can be found filtering on logic apps.