Return code when using Webhook's "Response Data" feature

Hi,

I need to return a response code to Webhook for a bot to work.

So far I’ve been able to do this just by putting a code in the “Response Data:” field:

{
  "replies": [
    {
      "message":"Response OK"
    }
  ]
}

However, I wanted to use functions from other nodes ahead, but he can’t get the result.

ex:

{
  "replies":[
    {
      "message":"Response: {{$node["HASH"].parameter["functionCode"]}}"
    }
  ]
}

How could I do that? because in my tests, it returns only the encoding and not the result.

tks

Sorry, do not understand what exactly you want to do. But it is not possible to get data from nodes “ahead” as that nodes would not have been executed yet and would so not have any data.

Anyway here a basic example that returns custom data from a previous node:

{
  "nodes": [
    {
      "parameters": {
        "path": "771b7447-759b-490e-b93e-8e4374077808",
        "responseMode": "lastNode",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        500,
        300
      ],
      "webhookId": "771b7447-759b-490e-b93e-8e4374077808"
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      myVariable: 'whatever you want to return',\n    }\n  }\n];"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "message",
              "value": "=Response: {{$json[\"myVariable\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        900,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Hi, thanks again for the reply.

I did not get it yet.

See the example:

{
  "nodes": [
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "message",
              "value": "=TEST OK"
            }
          ]
        },
        "options": {}
      },
      "name": "set message",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        750,
        550
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n  replies: [\n    {\n      \"message\":\"SET HERE\"\n    }\n  ]\n}\n  }\n];\n"
      },
      "name": "json",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        950,
        550
      ]
    }
  ],
  "connections": {
    "set message": {
      "main": [
        [
          {
            "node": "json",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I want to display the result of the set message node (TEST OK) on line 6 of the json node (SET HERE).

tried in many ways and failed.

In short, I need to display a result of the set message inside json, how to do this?

Honestly still do not understand how exactly the response should look like but here are two different version. Activate only one Webhook-Node at a time, else it will cause problems:

{
  "nodes": [
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "message",
              "value": "=TEST OK"
            }
          ]
        },
        "options": {}
      },
      "name": "set message",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        500
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n  replies: [\n    {\n      \"message\": $json.message,\n    }\n  ]\n}\n  }\n];\n"
      },
      "name": "json",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        500
      ]
    },
    {
      "parameters": {
        "path": "ee375356-bdd3-4707-a495-4db5ceeaa819",
        "responseMode": "lastNode",
        "options": {
          "responsePropertyName": "replies[0].message"
        }
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        500
      ],
      "webhookId": "ee375356-bdd3-4707-a495-4db5ceeaa819",
      "disabled": true
    },
    {
      "parameters": {
        "path": "ee375356-bdd3-4707-a495-4db5ceeaa819",
        "responseMode": "lastNode",
        "options": {}
      },
      "name": "Webhook1",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        650
      ],
      "webhookId": "981a6b6d-ce09-4e65-9141-b9c2ec262466"
    }
  ],
  "connections": {
    "set message": {
      "main": [
        [
          {
            "node": "json",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "set message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook1": {
      "main": [
        [
          {
            "node": "set message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hope that helps!

1 Like

thanks! that was exactly it.

you are amazing :clap:

Glad to hear that it helped. Have fun!

1 Like