paint-brush
How To Automate Your Processes Using HTTP Request Node From n8n by@sm.amudhan
1,931 reads
1,931 reads

How To Automate Your Processes Using HTTP Request Node From n8n

by AmudhanAugust 16th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How To Automate Your Processes Using n8n: Getting a web page and extracting data from it via a GET request. Creating a cloud VM via a POST request. Performing speech-to-text by sending an audio file via a request. The HTTP Request node can send any type of request, making it useful to talk to any RESTful API. This makes the request node one of the most versatile and powerful nodes in the n8s ecosystem. This tutorial assumes that you already have n8N set up and running.

Company Mentioned

Mention Thumbnail
featured image - How To Automate Your Processes Using HTTP Request Node From n8n
Amudhan HackerNoon profile picture

With over 150 nodes, n8n saves countless hours by automating repetitive tasks. But what happens when n8n doesn’t have a node for a tool you love?

Enter the HTTP Request node. This node can send any type of HTTP request, making it useful to talk to any RESTful API. This makes the HTTP Request node one of the most versatile and powerful nodes in the n8n ecosystem.

In this article, we are going to explore three examples to showcase how the HTTP Request node can be used in your workflow to automate tasks:

  1. Getting a web page and extracting data from it via a GET request
  2. Creating a cloud VM via a POST request
  3. Performing speech-to-text by sending an audio file via a POST request

Getting Ready

Setting up n8n

This tutorial assumes that you already have n8n set up and running. In case you don’t, you can install with npm:

npm install n8n -g

After the installation, start n8n by typing:

n8n start --tunnel
Note: This is only meant for local development and testing. For information on how to deploy n8n in a production environment, please refer to the n8n setup guide.

Workflow 1 — Getting a Webpage and Extracting Data

For this example, I’ll be retrieving the homepage of Hackernoon and extracting the names and URLs of all the articles.

You can also follow along this example by copying the workflow from n8n.io and making appropriate changes at each step.

Add a new HTTP Request node by clicking the + button at the top right of the Editor UI and selecting the HTTP Request node from the list. Then, select the GET option for Request Method.

Enter

https://hackernoon.com
as the URL. Set the Response Format to ‘String’ and then click on Execute Node. You’ll see that the source code of the page has been retrieved by n8n.

Next, add a HTML Extract node and connect it to the HTTP Request node. Select ‘JSON’ for Source Data. Set a Key of your choice — this is the name under which the extracted titles will be saved. Next, enter the CSS class name of the element you want to extract data from. In our case, all the blog titles are

h2
elements, so I put
h2
as the CSS Selector. Select ‘HTML’ as the Return Value and set Return Array to ‘True’ (make the slider green).

Add another HTML Extract node and connect it to the previous HTML Extract node. Select ‘JSON’ for Source Data. Click Add Value under the ‘Extraction Values’ section and add a value with the Key as

title
, CSS Selector as
a
, and the Return Value as ‘Text’. Similarly, add another entry in the Extraction Values section, with the Key as
url
, CSS Selector as
a
, the Return Value as ‘Attribute’, and Attribute as
href
.

Now click on Execute Node and you will see the titles and URLs of each blog post.

Here’s a GIF of me following the steps above.

Workflow 2 — Creating a cloud instance on DigitalOcean

For this example, we will be creating a cloud instance on DigitalOcean, a leading web hosting provider.

You can also follow along this example by copying the workflow from n8n.io and making appropriate changes at each step.

DigitalOcean’s API allows the creation of new droplets via a POST Request, which we will be using, in conjunction with the HTTP Request node.

First, sign up for an account on DigitalOcean’s website. Then, once you’re logged in, generate and copy your ‘Personal access token’ from the API section. This is what we will use to authenticate against the API in n8n.

Now, head over to n8n and create a new workflow. Next, add a HTTP Request node and select POST as the Request Method. Type in

https://api.digitalocean.com/v2/droplets
as the URL. Set the Response Format to ‘JSON’. Add a Body Content Type option and select ‘JSON’.

It’s time to add Body Parameters to the request. These parameters tell DigitalOcean what kind of a droplet to create. I chose to add four basic parameters, but you can add several different parameters (Refer: DigitalOcean docs).

You can add a parameter by clicking the Add Parameter button and adding in the names and values as shown below.

┌────────┬──────────────────┐
│  Name  │      Value       │
├────────┼──────────────────┤
│ name   │ n8n-rocks        │
│ region │ blr1             │
│ size   │ s-1vcpu-1gb      │
│ image  │ ubuntu-20-04-x64 │
└────────┴──────────────────┘

Next, we will add the authorization header. Scroll down to the Headers section and add a new Header with the name ‘Authorization’ and the value as

Bearer {your_DO_token}
.

Now click ‘Execute Node’ and head over to your DigitalOcean console. You should see that a new droplet with the name

n8n-rocks
has been created.

Here’s a GIF of me following the steps above.

Workflow 3 — Speech to text conversion using Wit.ai

For this example, we will converting speech to text from audio files using Wit.ai. We will send audio files and receive the detected text via the HTTP API.

You can also follow along this example by copying the workflow from n8n.io and this audio sample, making appropriate changes at each step.

First, sign up for an account on Wit.ai. Create a New App. Then, head over to Settings and copy your ‘Server Access Token’. We will use this later when setting up the node credentials in n8n.

Now, head over to n8n and create a new workflow. Add a Read Binary File node. Then, enter the File Path to your audio sample and choose a Property Name.

Then, add a new HTTP Request node and connect both nodes. Select ‘POST’ as the Request Method. Type in

https://api.wit.ai/speech?v=20200513
as the URL. Then, select ‘JSON’ as the Response Format. Select JSON/RAW Parameters as ‘True’ (make the slider green). Next, add a Body Content Type option, selecting ‘RAW/Custom’.

Select Send Binary Data as ‘True’ and enter the property name that you picked earlier. In the Headers section, enter the following, replacing

{your_token_goes_here}
with the ‘Server Access Token’ you copied from Wit.ai.

{{JSON.parse('{"Authorization":"Bearer {your_token_goes_here}", "Content-Type":"audio/wav"}')}}

Now click ‘Execute Node’ and in a couple of seconds, you should see the detected text returned from Wit.ai.

Here’s a GIF of me following the steps above.

Further exploration

Since most of the internet functions on HTTP requests; you can use n8n to integrate any platform that supports a RESTful API using a similar set of steps (consult the documentation) for the appropriate settings.

You can also use n8n to automate cross-platform tasks, by integrating several platforms into your workflow.

For example, the data extracted from the webpage could be saved to a Google Sheet by adding a node to the workflow. The text detected using Wit.ai could be connected to Google’s Translate API using the HTTP Request node and translated to a different languages!

Conclusion

In this tutorial, we learnt to use the HTTP Request node to extract information from the web, create a cloud server and convert speech to text. We also discussed the possibility of using the HTTP Request node with other RESTful APIs and the versatility in doing so, most of the web functions on HTTP requests.

Much like people, technology works better as a collective. n8n enables you to integrate a tool with any other tool, enabling cross-platform workflows like never before. The possibilities are endless. What will you automate?

In case you’ve run into an issue while following the tutorial, feel free to reach out to me on LinkedIn or ask for help on our forum 💙

Previously published at https://medium.com/n8n-io/http-request-node-the-swiss-army-knife-b14e22283383