I need to learn how to work some data in my custom activity backend. - The issue is that I'm not sure how to grab the data in backend once activity has posted data to my execute endpoint.
My backend activity js is below. - If you scroll down to my execute function you will see what I am trying to do.
I simply want to take the data from my inArguments[0] and then post them to another endpoint.
So far I have been unsuccessful in figuring out how to grab the data once it's been posted to /execute endpoint. I can confirm the client side of my activity is working properly because I am able to post the payload from JB to postBin, however, when I send it to my own endpoint e.g. (https://someactivityurl.herokuapp.com/journeybuilder/execute) I have no idea how to grab the data from there.
Any advice here would be greatly appreciated!
"use strict";
var util = require("util");
// Deps
const Path = require("path");
const JWT = require(Path.join(__dirname, "..", "lib", "jwtDecoder.js"));
var util = require("util");
var http = require("https");
var request = require("request");
exports.logExecuteData = [];
function logData(req) {
exports.logExecuteData.push({
body: req.body,
headers: req.headers,
trailers: req.trailers,
method: req.method,
url: req.url,
params: req.params,
query: req.query,
route: req.route,
cookies: req.cookies,
ip: req.ip,
path: req.path,
host: req.host,
fresh: req.fresh,
stale: req.stale,
protocol: req.protocol,
secure: req.secure,
originalUrl: req.originalUrl,
});
console.log("body: " + util.inspect(req.body));
console.log("headers: " + req.headers);
console.log("trailers: " + req.trailers);
console.log("method: " + req.method);
console.log("url: " + req.url);
console.log("params: " + util.inspect(req.params));
console.log("query: " + util.inspect(req.query));
console.log("route: " + req.route);
console.log("cookies: " + req.cookies);
console.log("ip: " + req.ip);
console.log("path: " + req.path);
console.log("host: " + req.host);
console.log("fresh: " + req.fresh);
console.log("stale: " + req.stale);
console.log("protocol: " + req.protocol);
console.log("secure: " + req.secure);
console.log("originalUrl: " + req.originalUrl);
}
/*
* POST Handler for / route of Activity (this is the edit route).
*/
exports.edit = function (req, res) {
// Data from the req and put it in an array accessible to the main app.
//console.log( req.body );
logData(req);
res.send(200, "Edit");
};
/*
* POST Handler for /save/ route of Activity.
*/
exports.save = function (req, res) {
// Data from the req and put it in an array accessible to the main app.
//console.log( req.body );
logData(req);
res.send(200, "Save");
};
/*
* POST Handler for /execute/ route of Activity.
*/
exports.execute = function (req, res) {
// example on how to decode JWT
JWT(req.body, process.env.jwtSecret, (err, decoded) => {
// verification error -> unauthorized request
if (err) {
console.error(err);
return res.status(401).end();
}
if (decoded && decoded.inArguments && decoded.inArguments.length > 0) {
// decoded in arguments
var decodedArgs = decoded.inArguments[0];
var options = {
method: "POST",
url: "https://someapi",
headers: {
Authorization:
"Bearer somekey",
"Content-Type": "application/json",
},
body: JSON.stringify(decodedArgs),
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
logData(req);
res.send(200, "Execute");
} else {
console.error("inArguments invalid.");
return res.status(400).end();
}
});
};
/*
* POST Handler for /publish/ route of Activity.
*/
exports.publish = function (req, res) {
// Data from the req and put it in an array accessible to the main app.
//console.log( req.body );
logData(req);
res.send(200, "Publish");
};
/*
* POST Handler for /validate/ route of Activity.
*/
exports.validate = function (req, res) {
// Data from the req and put it in an array accessible to the main app.
//console.log( req.body );
logData(req);
res.send(200, "Validate");
};
jwtDecoder.js file
'use strict';
module.exports = (body, secret, cb) => {
if (!body) {
return cb(new Error('invalid jwtdata'));
}
require('jsonwebtoken').verify(body.toString('utf8'), secret, {
algorithm: 'HS256'
}, cb);
};