Dears,
I am new to n8n and i have a beginner question regarding webhook I would like to change the response code of a webhook based on some business logic criteria. For example:
- If entry with some id allready exists, webhook should return http 400
- If entry with some id doies not exists, insert the recort into database and return http 200
I have tried to set the status code in Function and then reference its ouotput in webhook. But it is not working (probbably due to execution order):
{
"name": "POC_http_code",
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"position": [
250,
300
],
"typeVersion": 1
},
{
"parameters": {
"httpMethod": "POST",
"path": "02da24c1-b93f-488a-9fac-b0c5bd2c1270",
"responseCode": "={{$node[\"MockLogic\"].json[\"statusCode\"]}}",
"responseMode": "lastNode",
"responseData": "allEntries",
"options": {}
},
"name": "Webhook",
"type": "n8n-nodes-base.webhook",
"position": [
450,
450
],
"webhookId": "02da24c1-b93f-488a-9fac-b0c5bd2c1270",
"typeVersion": 1
},
{
"parameters": {
"functionCode": "var id = items[0].json.body.id;\n\nif (id > 5) {\n items[0].json.statusCode = 400\n} else {\n items[0].json.statusCode = 200\n}\n\nreturn items;"
},
"name": "MockLogic",
"type": "n8n-nodes-base.function",
"position": [
650,
450
],
"typeVersion": 1
}
],
"connections": {
"Webhook": {
"main": [
[
{
"node": "MockLogic",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {},
"id": "36"
}
I have also tried to use Set component without sucess. Can anyone point me to right direction? Thanks!