Switch NodeJS Webhook not reaching Switch

Post Reply
cordell_printnow
Newbie
Posts: 4
Joined: Mon Aug 28, 2023 9:38 pm

Switch NodeJS Webhook not reaching Switch

Post by cordell_printnow »

Hello, I am having issues with the nodeJS example script to receive webhooks and send a response.

I have a flow where I have the built-in app and a NodeJS script both trying to use Webhooks.

Here's the NodeJS Script I'm using:
const fs = require('fs-extra');
const os = require('os');

async function flowStartTriggered(s, flowElement) {
try {
await s.httpRequestSubscribe(HttpRequest.Method.POST, '/test');
}
catch (error) {
await flowElement.failProcess("Failed to subscribe to the request %1", error.message);
}
}

async function httpRequestTriggeredSync(request, args, response, s) {

let requestBody = JSON.parse(request.getBodyAsString());
let requestHeaders = request.headers;

await flowElement.log(LogLevel.Warning, `Request Body: ${JSON.stringify(requestBody)}`);
await flowElement.log(LogLevel.Warning, `Request Headers: ${JSON.stringify(requestHeaders)}`);
await flowElement.log(LogLevel.Warning, `Response Object Names: ${JSON.stringify(Object.getOwnPropertyNames(response))}`);
await flowElement.log(LogLevel.Warning, `Response Object Descriptors: ${JSON.stringify(Object.getOwnPropertyDescriptors(response))}`);

response.setStatusCode(200);
response.setBody(Buffer.from({'status':'true'}));
return;
}

async function httpRequestTriggeredAsync(request, args, s, flowElement) {
let requestBody = JSON.parse(request.getBodyAsString());

let tmpFileName = `${await os.tmpdir()}/tmpwebhookfile.json'`;
await fs.writeJson(tmpFileName, requestBody);
let tmpJob = await flowElement.createJob(tmpFileName);
await tmpJob.sendToSingle();
await fs.remove(tmpJob);
}
According to this, I should be able to send data to: https://{MYHOST}:51088/scripting/test

When I start the script, I see the following text in the message board on the switch server:
08:43:32 - WebhookFlowTest - Webhook - Subscribed to requests for the path /testApp
08:43:32 - WebhookFlowTest - Webhook - the Request Method is: POST
08:43:32 - WebhookFlowTest - Webhook - the Protocol Type is : HTTPS
08:43:32 - WebhookFlowTest - Webhook - The notification to 'ws://127.0.0.1:55095/api/v1/process' has been sent successfully. Waiting for the "processing finished" response...
08:43:32 - WebhookFlowTest - NodeJSWebhookTest - The notification to 'ws://127.0.0.1:55095/api/v1/process' has been sent successfully. Waiting for the "processing finished" response...

Using Postman to send and receive responses, the built-in Switch Webhook App will receive the webhook, create a JSON file and place it in a folder, it then returns the message:
{"status": "true"}

The NodeJSWebhookTest returns:
{
"status": false,
"error": "Cannot send webhook response: TypeError [ERR_INVALID_URL]: Invalid URL"
}

Yet, if I send the data to a wrong URL, I get:
{
"status": false,
"error": "Subscription cannot be found."
}

The app in Switch says the webhook is Version 7.
I am running Fall 2022 version of Switch. I just upgraded this week.
This webhook WAS WORKING in Spring 2022.
jan_suhr
Advanced member
Posts: 592
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Switch NodeJS Webhook not reaching Switch

Post by jan_suhr »

The Webhook app has had some problems and if it worked in earlier version you could use the older version of the app. Note that it is the Legacy scripting then and it has a other port number for the call.

Look in My Enfocus and you can select the older version
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: Switch NodeJS Webhook not reaching Switch

Post by freddyp »

When sending to a webhook that is started by a NodeJS script the URL that has to be defined on the sending side is different than what is was in legacy.

The default Webhook app v6 is legacy, as of v7 it is NodeJS. When you log in on MyEnfocus and you check the default apps on your Switch and you look at the documentation of v7 you will find the explanation. It is also described in the NodeJS scripting documentation:
https://www.enfocus.com/manuals/Develop ... oints.html
Post Reply