I'm using an azure function app to get data from one location and post it to another location. I using axios but keep getting 401 errors. No one seems to know why this is happening. Below is my code. Any ideas?
module.exports = async function (context, req) {
var axios = require('axios').default;
var url = 'https://api.fulcrumapp.com/api/v2/records.json';
// context.log("CONTEXTLOG:" ,context);
// context.log("REQ", req);
// context.log("REQ.BODY", req.body);
async function payloadProcessor(payload) {
let result = null;
if (payload && payload.data && payload.data.form_id && payload.data.form_id === '1e3f56c7-59e2-43b2-8ca6-e6833a91493c') {
if (payload.type === 'record.create') {
context.log('Im going to create here @@@@@@@@@@@@@@@@@@@@@@@@@@2');
result = await createRecord(payload);
} else {
context.log('Nothing to do @@@@@@@@@@@@@@@@@@@@');
}
// else if (payload.type === "record.update") {
// updateRecord(payload);
// } else if (payload.type === "record.delete") {
// deleteRecord(payload);
// }
}
else {
context.log(`${payload} Condition didnt match our response @@@@@@@@@@@@@@@@@@@@`);
}
return result;
}
context.log('@@@@@@@@@@@@@@@@@@@@ About to call payloadProcessor @@@@@@@@@@@@@@@@@@@@');
await payloadProcessor(req.body);
context.log('@@@@@@@@@@@@@@@@@@@@ Finsihed calling payloadprocessor @@@@@@@@@@@@@@@@@@@@');
async function createRecord(payload) {
var data = payload.data;
context.log(data);
data.form_id = 'a27d49cc-96b9-431d-a105-1b8056bc8e76';
data.form_values['4d79'] = data.form_values['4d79'];
context.log('got payload @@@@@@@@@@@@@@@@@@@@');
context.log(url);
//FAILING RIGHT HERE
var response = await axios.post(url, data, {
'Content-Type': 'application/json',
'Authorization': `Basic ${token}`
});
// context.log(response);
context.log("Posted Form data here @@@@@@@@@@@@@@@@@@@@");
return response;
}
context.res = {
status: 200,
body: {
result: "Successful"
}
};
};