Level Of Difficulty: Intermediate
Welcome to this tutorial where you will learn how to make an HTTP POST request and send data to an API endpoint.
For this tutorial let’s use a web service called short.io as an example API for performing requests to. This is an advanced URL shortener service with an API you can use in your scenarios.
In order to use this service you first need to create an account.
Once your account is successfully created a subdomain will automatically be assigned to your account.
As you can see, for this example the subdomain is integromatexample.shortcm.li . If you are planning on using this service regularly you can also assign a custom domain to your account.
Once you are inside your account dashboard you can open the sidebar menu and select the Integrations & API option. On this page you will be provided with an API key that you can use in order to perform various GET and POST requests.
Now, let’s take a look at the API documentation in order to get an idea of the endpoints available and the requests that can be made.
As you can see on the sidebar, the documentation page is split up into different sections.
For this example let’s navigate over to the ‘link editing’ section and click on ‘Create URL’.
This is a POST request that will allow you to shorten a long URL.
However, together with the long URL you can provide many other parameters such as a duration before the short URL expires, an iphoneURL which is where the user will be redirected if they are on an iphone device and a lot more.
On the right hand side, you can see an example request being made to this endpoint using different programming languages. Below you can see the response of the request.
Note that with many APIs you decide to use, the request examples provided on the documentation page might not be very clear, especially if you are not familiar with the programming languages provided.
You don’t have to worry though, because if you look closely, the fields and parameters that these requests have are also part of the HTTP app in Integromat.
Let’s see this in action, by constructing this exact request inside Integromat using the HTTP module.
So the first item required in this request is the request method…. which in this case is POST.
The second item required is the URL.
The third, fourth and fifth items are actually header items.
The idea is that every request you make also includes a header. You can think of a header as another part of the request that you can add data to. This data is hidden and is not added to the request URL.
In this example, the header needs to include an accept: application/json parameter which indicates that the response of the request needs to be of data type JSON in order to be accepted.
Let’s add this parameter to the request. The name will be accept and the value will be application/json.
The second header parameter is the authorization key or in other words the API key. Let’s add this to the request as well.
The name will be authorization and the key will be the one generated inside the API dashboard of your shortcm account.
The third and final header parameter is the content-type, which indicates the data type of the content that will be passed along this request.
In this case the data type will be JSON. Let’s add this parameter to the request.
Perfect! The request header is now ready.
The final element is the data. This is where you can define all the parameters such as the URL to be shortened, the domain you want to use and so on. This data needs to be passed to the API request as a JSON object, as you can see below.
On your Integromat HTTP app, the first thing you need to do is select the body type. In this case the best body type will be Raw, which will then open up a new selector, Content Type.
As specified in the header of this request, the content type of this request is JSON, therefore the correct selection will be JSON.
Finally on the input box below you can copy/paste the sample request body from the documentation page.
As you can see the first part of each parameter denotes the name of the parameter and the second part denotes the value.
Both the name and the value are surrounded by quotation marks and they are split up with a semicolon in the middle.
The easiest way to add new parameters to this request is by visually assigning all the values on the interactive documentation page. This will generate a JSON object which can then be copied over to the body of the request.
Let’s assign the domain to the one created with this demo account. Let’s also assign the OriginalURL to https://www.google.com. And finally, let’s give this link a title Google.
As you can see the JSON object was automatically generated and can now be transferred over to the HTTP module.
The request is almost ready. The final thing to do is make sure to select the Parse Response option to make sure that the response from the server is automatically converted to an Integromat friendly format, with data elements that can be mapped in subsequent modules.
You can now save this scenario and execute it once to see if everything works as planned.
This request should create a short URL using the assigned domain that when visited should redirect the user to google.com
Once the scenario executes you can click on the HTTP request bubble icon to see the response of this request.
A short URL should have been successfully created and once opened in a browser should redirect you to google.com.