Looping http request node

Is there any way to create a loop with http request node, to pass a different set of values with subsequent iterations.

The most time there should be no need for looping anything in n8n as the nodes normally execute once for every item they receive. So also the HTTP Request Node. If you give it 3 items it will make 3 requests.

Here a simple example (you can simply copy and paste it in n8n)

What if i want to hit different api’s with the http request node simultaneously or subsequently(with one node).
The url would be the same for example https://sample.example.net/api/users/{id}, just the id in the url would be different eg,https://sample.example.net/api/users/001,https://sample.example.net/api/users/002.
What i want to achieve here is a way to feed this “id” using add expression, and i want to do this for all the values of “id”.

Yes that is no problem. Simply set the URL as expression and also everything else you want to change depending on the input data.

What I posted was just a very simply example to show how it works.

Hi Jan. I am having this problem. I’m not sure how to easily set the URL as an expression so that the http request changes the URL id on each iteration.

For example if what I’m passing to the request is an json object and I want to iterate through the first to third Item what do I write as the expression? Selecting the first ID for example (as seen in the photo) will work and allow me to use the zeroth index API endpoint, however, it won’t then change to the first and second index.

Hey @jason123

The UI just shows the first item but when you run the workflow it’s gonna iterate over all items.

1 Like

Thanks for your response, however, when I run it is still only doing the one put request and still not iterating over all the items for me. What am I doing wrong?

Hm if it does only make one request it sounds like to me like there is really just one item.

Does it display for you in the detail-view ion the top after “Results:” also just “1” or really “2”?

Ah yes you are correct. I was passing data from function item to the http request. While this was the case I have been trying to change data from a GET request which returns as seen in the photo below, and then pushing that changed data back to the database.

However, in the function node I can’t figure out how to access the zeroth object (the object with the label 0 at the top). I assumed it would be items[0] but that doesn’t work.

Photo of me trying to return the first index returns nothing

.

Returning simply items returns the same as the GET request response. Also items.length returns nothing.

Sorry for the many questions but thank you for the quick responses.

@jason123 Try it like this

The index is 0 based. So $item(0) will return the first item, $item(1) the second one, …

But looks like you are not passing the data to the http node correctly. Gotta be with the following Format

[{ json: { your data } } ]

@jason123 could you provide an example of the function node which is passing the data to the http request so that I can map it correctly and share it with you?

@jason123 with this example you should be able to figure it out. Run the workflow and check here Webhook.site - Test, process and transform emails and HTTP requests. You will see that is sending two requests with different values.

Let me know if that helps.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n    {\n      json: {\n        name: \"ricardo\",\n        type: \"blabla\",\n        status: \"active\"\n      }\n    },\n    {\n      json: {\n        name: \"maria\",\n        type: \"epale\",\n        status: \"inactive\"\n      }\n    }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://webhook.site/728e1d7c-f33d-49e2-91df-67e158f92c49",
        "options": {},
        "queryParametersUi": {
          "parameter": [
            {
              "name": "name",
              "value": "={{$node[\"Function\"].data[\"name\"]}}"
            }
          ]
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thank for your help Ricardo. So I first collect data from a GET request. I think I understand now the data has to have a specific structure in order to for the next PUT request to action multiple items, however, I’m confused on how to manipulate the incoming GET request data so that I can transform it into a structure like the one you showed down below. When I do $items(0) it says that the

ReferenceError: $items is not defined

Just use items.

So let’s say the get request responded something like this

{
   "users":[
      {
         "name":"ricardo"
      },
      {
         "name":"jason"
      }
   ]
}

To map that data to something n8n “understands” the function node should be like this:

    const data = []
    for (const user of items[0].json.users) {
        data.push({
        	json: {
    			name: user.name
      		}
    	})
    }
    return data;

this is a working example:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://webhook.site/728e1d7c-f33d-49e2-91df-67e158f92c49",
        "options": {},
        "queryParametersUi": {
          "parameter": [
            {
              "name": "name",
              "value": "= {{$node[\"Function\"].data[\"name\"]}}"
            }
          ]
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        890,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n    {\n      json: {\n        users: [ \n          {\n            name: \"ricardo\"\n          },\n          {\n            name: \"jason\"\n          }\n        ]\n      }\n    }\n]"
      },
      "name": "HTTP GET MOCKUP",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        480,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "    const data = []\n    for (const user of items[0].json.users) {\n        data.push({\n        \tjson: {\n    \t\t\tname: user.name\n      \t\t}\n    \t})\n    }\n    return data;\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        670,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP GET MOCKUP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP GET MOCKUP": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

More info about that can be found here: Introduction | Docs

@jason123 let me know if that helps. We are happy to help.

2 Likes

Thanks so much for getting back to me. Unfortunately haven’t had time to test it out. Will get back to you once I’ve tried it. But looks good.

1 Like

Hey @jason123, did it work?

Yes works now. Thanks for your help! Sorry for delayed response.

1 Like

Hey Guys

Having the same issue having the http request loop for each item on the input.
Currently the input has 5 rows that need to be queried via rest api.

When using the get or put feature to get or update a item, the swagger states to use the following

https://myapi.com/assignments/{ID}

So i have my node url set up as an expression where the reference number refers to the ID i need to query.
https://myapi.com/assignments/{{ $json.referencenumber}}

When i run this the out put only returns the data from the item in the first row of the table but does not continue to show the rest and it does not provide any errors.

I have another open ticket here if someone could point out where i went wrong?
Query api multiple times from results on input table - Questions - n8n

Cheers