question

MikeGrilli-5705 avatar image
0 Votes"
MikeGrilli-5705 asked MayankBargali-MSFT answered

Cant post axios 401 error

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MayankBargali-MSFT avatar image
0 Votes"
MayankBargali-MSFT answered

@MikeGrilli-5705 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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.