javascriptnode.jsjsonpulseqtest

Nodejs - Passing json object to a event.handler function


Apologies in advance as this is my first time working with NodeJs.

We are using a product called QTest Pulse. In simple terms we have a webhook that is hooked to nodejs file that gets executed on the server and produces results. I am trying to setup my local environment to debug the file to make changes to it.

We usually call the file using a bash script like below

curl -X POST \
    https://pulse-7.qtestnet.com/webhook/ \
    -H 'cache-control: no-cache' \
    -H 'content-type: application/json' \
    -d @payload.json

And the payload.json contains data like below

{"testcycle" : "178173", "result" : "encrypted data", "projectId" : "90354"}

The file on the server parser.js contains

function processPayload(payload) {
    const projectId = payload.projectId;
    const cycleId = payload.testcycle;

    console.log(projectId);
    console.log(cycleId);
}

exports.handler = function (
    {event: body, constants, triggers},
    context,
    callback
) {
    const formattedResults = processPayload(body);
};

I tried to create a file called runner.js to invoke the event in parser.js

const parser = require('./Parser');
const fs = require('fs');

const jsonContent = fs.readFileSync('payload.json').toString;
let output = parser.handler({body: jsonContent, constants: "", triggers: ""}, "", "");

console.log(output);

When I run this, I always get body inside parser.js as undefined. I have also tried to hard code the value instead of reading from the file, Still get the same issue. Thanks in advance.


Solution

  • 2 Mistakes:

    1. typo: fs.readFileSync('payload.json').toString you forget to call toString()

    2. You are doing wrong destructure. You are already sending body.

      exports.handler = function ( {body, constants, triggers}, context, callback ) { const formattedResults = processPayload(body); };