Iterating through a list and execute command (curl) for each item

Describe the issue/error/question

Hello, I have a workflow that pulls a payload and processes the information from the payload, anywhere from 1 to n messages. The workflow uses a functionItem node to regex out info and into a list of sentences cleanly describing each event and ships it off to slack to alert my team. One of the pieces of info is an IP address. Id like to curl each address with ipinfo.io/ipaddress. Im using the execute command but I can only pull one IP address ipaddresslist[n]. Basically, I need to iterate through every ip address within the payload, execute a curl http://ipinfo.io/ipaddresslist[n] and then be able to access that list to pair with the list of info/alert sentences.

I dont think I can do this within a functionItem node, am I wrong?

Thanks,

Chandler

Generally, does each node automatically execute once per item. Meaning if each of the IP addresses is already in a separate item, then it will already work without you having to do anything.
It is correct that the preview (and when you create an expression) will only display you one IP address but once you execute the workflow, you will see that it processes all of them.

Just two more things that I think are important to mention:

  1. It is probably a better idea to use the HTTP Request node instead of the Execute Command node + curl, as that node is specifically made for that kind of use cases
  2. If the IP addresses are not already in a separate item, you can use the Item Lists node to separate them

So PaperTrail sends any logs that match whats configured to be grabbed in a given minute over to the workflow in one giant payload. In our live environment its usually not more than 1 or 2 but for sending test data PaperTrail sends 50 logs per payload.

The items within that payload each have a message, and I have to regex the necessary info out of each message, one being the IP address which is added to a list. Once I have that list I then need to iterate through those list items and execute a curl for each. So from a workflow perspective I dont see how to create a list and then iterate through the given list, while executing a curl on each iteration once per workflow.

Hi @pccurnow, transferring your requirements into an n8n workflow would look something like this:

  1. Receiving papertrail data (not sure how this would look like - is this service sending a webhook or are you fetching data here? Mabye you can provide an example.)
  2. Split the list up into individual n8n items as suggested by @jan
  3. Applying the regex you have in mind, for example using an n8n expression with JavaScript’s .match() inside the Set node
  4. Use the HTTP Request node to send a request to each IP you have extracted instead of curl.

So the workflow would look roughly like this:

As you can see the initial node representing your data returns only 1 item, but the Item Lists node splits this into individual items. All subsequent nodes will then run once for each item.

Thanks @MutedJam, I will give this a try and keep yall posted.

1 Like