api call from flow
Posted: Tue Sep 01, 2020 10:14 am
I am currently trying to make an api call from a flow. I already have a script that works using Javascript but for this I have decided to put my poor NodeJs skills to the test and see how I go. I have failed so far, it appears that the http code is just ignored by switch. I do not get success logs or error logs and in debug mode it just jumps over the code.
Below I have shared my code, you will notice I have both http and axios in the code, basically I was just trying both to see if one would work. Originally I just had http but expanded when I could not get it to work.
Help will be appreciated.
const http = require("http");
const axios = require('axios');
const DomPaser = require("xmldom").DOMParser;
const XPath = require("xpath");
const fs = require("fs-extra");
async function jobArrived(s, flowElement, job) {
let jobName = await job.getName();
const tempPath = await job.getDataset("knifedata", AccessLevel.ReadOnly);
const content = await fs.readFile(tempPath, { encoding: "utf8" });
var doc = new DomPaser().parseFromString(content);
var value = await XPath.select("//value",doc);
await job.log(LogLevel.Info, jobName);
let filename = jobName;
let data = JSON.stringify({ details: { "knifenumber": value[0].firstChild.data, "filename": filename, "description1": isEmpty(value[2]), "description2": isEmpty(value[3]), "description3": isEmpty(value[4]), "jobnumber":value[1].firstChild.data} })
let options = {
hostname: "myhost",
port:80,
path: "/api/iq/NewKnife",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(data)
}
}
axios
.post('myhost/api/iq/NewKnife', {
item: data
})
.then(res => {
job.log(LogLevel.Info,"statusCode: %1",[res.statusCode])
job.log(LogLevel.Info,res)
})
.catch(error => {
job.error(LogLevel.Info,error)
})
http.request(options, res => {
job.log(LogLevel.Info,"statusCode: %1",[res.statusCode])
res.on('data', d => {
data += d
})
res.on("end", () => {
console.log(data)
})
.on("error", console.error)
.end(data)
})
await job.sendToSingle();
}
var isEmpty = function(value) {
if (value.firstChild) {
return value.firstChild.data;
}else{
return "";
}
}
Below I have shared my code, you will notice I have both http and axios in the code, basically I was just trying both to see if one would work. Originally I just had http but expanded when I could not get it to work.
Help will be appreciated.
const http = require("http");
const axios = require('axios');
const DomPaser = require("xmldom").DOMParser;
const XPath = require("xpath");
const fs = require("fs-extra");
async function jobArrived(s, flowElement, job) {
let jobName = await job.getName();
const tempPath = await job.getDataset("knifedata", AccessLevel.ReadOnly);
const content = await fs.readFile(tempPath, { encoding: "utf8" });
var doc = new DomPaser().parseFromString(content);
var value = await XPath.select("//value",doc);
await job.log(LogLevel.Info, jobName);
let filename = jobName;
let data = JSON.stringify({ details: { "knifenumber": value[0].firstChild.data, "filename": filename, "description1": isEmpty(value[2]), "description2": isEmpty(value[3]), "description3": isEmpty(value[4]), "jobnumber":value[1].firstChild.data} })
let options = {
hostname: "myhost",
port:80,
path: "/api/iq/NewKnife",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(data)
}
}
axios
.post('myhost/api/iq/NewKnife', {
item: data
})
.then(res => {
job.log(LogLevel.Info,"statusCode: %1",[res.statusCode])
job.log(LogLevel.Info,res)
})
.catch(error => {
job.error(LogLevel.Info,error)
})
http.request(options, res => {
job.log(LogLevel.Info,"statusCode: %1",[res.statusCode])
res.on('data', d => {
data += d
})
res.on("end", () => {
console.log(data)
})
.on("error", console.error)
.end(data)
})
await job.sendToSingle();
}
var isEmpty = function(value) {
if (value.firstChild) {
return value.firstChild.data;
}else{
return "";
}
}