Use Session in Node Http Request

I’m getting the session, then using http Request node, I would like to insert the data into my app by passing the session along with the data in the body.
How to pass the session in the Http Request node after the Function Item node that contains the list of items that need to be entered.
As the Node Function Item runs for each item, in every way I try it sends the session just in first the call.
I’ve already changed the order of the nodes, but I still can’t find a way with a single session to do multiple inserts using the Http Request node.
Has anyone ever experienced this? can you give me a light?

I’m sending some prints in my workflow

Hi @Edson_Rodrigues, so you just want to send the same sessionName body parameter for each item on your last HTTP node? I think this example would to the trick:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    sessionId: (+new Date).toString(36).slice(-5) + Math.random().toString(36).substr(2, 5)\n  }\n}];"
      },
      "name": "Set Dummy Session Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://reqres.in/api/users",
        "options": {},
        "queryParametersUi": {
          "parameter": [
            {
              "name": "page",
              "value": "2"
            }
          ]
        }
      },
      "name": "Get Users",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "data",
        "options": {}
      },
      "name": "Item Lists",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://hookb.in/JK1qDGGV9Gug0l990rP0",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "name",
              "value": "={{$json[\"first_name\"]}}"
            },
            {
              "name": "sessionName",
              "value": "={{$items(\"Set Dummy Session Data\")[0].json.sessionId}}"
            }
          ]
        }
      },
      "name": "HTTP Request (with SessionName)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Dummy Session Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Dummy Session Data": {
      "main": [
        [
          {
            "node": "Get Users",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Users": {
      "main": [
        [
          {
            "node": "Item Lists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Item Lists": {
      "main": [
        [
          {
            "node": "HTTP Request (with SessionName)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

It uses dummy data and then sends the requests to a Hookbin. The interesting part is the expression used on the very last node in my example to populate the sessionName field:

image

This uses $items(), which gives you access to all the items of the current or parent nodes. In this case, I am simply selecting the first item from my Set Dummy Session Data node using the 0. This is assuming you only get a single item with the session data.

Does this work for you? Or is your session data structured differently?

Hi @MutedJam.

I had already lost a few hours and couldn’t find a solution.
A simple solution and saved me.

I just changed the way node Http Request receives the session parameter as you mentioned.

{{$items(“GET-SESSION”)[0].json[“result”][“sessionName”]}}

Thank you for sharing.

1 Like

Excellent, I am glad to hear this is working for you. Thanks a lot for confirming!