Cant post axios 401 error

Mike Grilli 1 Reputation point
2022-05-25T18:29:53.1+00:00

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"
    }
};

};

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,321 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 68,746 Reputation points
    2022-05-26T00:49:46.48+00:00

    @Mike Grilli Thanks for reaching out. The issue is more towards how you are calling your fulcrumapp REST API. 401 error means that the request was not authorized correctly and looking into the fulcrumapp authorizations document I can see that the API Token need to pass using X-ApiToken HTTP request header.

    As per your code you are passing it as the Authorization header which is invalid to the fulcrumapp as per their document they support X-ApiToken

     var response = await axios.post(url, data, {  
             'Content-Type': 'application/json',  
             'Authorization': `Basic ${token}`  
         });  
    

    I will first suggest you to test the fulcrumapp REST endpoint using cURL comment to verify if you are passing the right header or you can use fiddler/postman to test the same.
    Once you have verified the right header and request then you can try testing the endpoint through your code.

    0 comments No comments