API Error
# 🤝help
f
Hello , I am getting error when using Axios in get request The error is : Error executing action "inline-card:Fetch data from a remote server using Basic Authentication..js" in flow:Main:node:QueryDatabase Error executing action "inline-card:Fetch data from a remote server using Basic Authentication..js" in flow:Main:node:QueryDatabase [Error, Check dependency list! Synchronous require cannot resolve module 'axios'. This is the first mention of this module!] {"scope":"bot-action","cardId":"card:Fetch data from a remote server using Basic Authentication.","nodeId":"node:QueryDatabase","workflowId":"flow:Main"} Code I am using is const axios = require('axios'); const https = require('https'); const data = captured on botpress; async function fetchData() { try { const username = 'username'; const password = 'password'; and password const authString = Buffer.from(${username}:${password}).toString('base64'); const response = await axios.get(https:///api//extinet/web/${data}, { headers: { 'Authorization': Basic ${authString} // Add the Authorization header with the authString }, httpsAgent: new https.Agent({ rejectUnauthorized: false }) // Ignore SSL certificate validation (optional) }); // Handle the response data console.log('Response data:', response.data); // You can do further processing with the response data here } catch (error) { // Handle any errors console.error('Error fetching data:', error); } } // Call the function to fetch the data fetchData();
f
Hey there, You don't have to require Axios. Botpress does that for you so you can just delete that line
f
Hello @fresh-fireman-491 , Do I need to use : const https = require('https');
f
I don't think so. You could try without it
Depends on if Botpress is already requiring it for you
If not then you will need it
But it might not be white listed, then it won't work. Why do you need it?
f
I want the response of the content which is hosted externally
with Https Still there I am getting this error Error executing action "inline-card:Fetch Fixlet Data with Basic Authentication and HTTPS.js" in flow:Main:node:QueryDatabase Error executing action "inline-card:Fetch Fixlet Data with Basic Authentication and HTTPS.js" in flow:Main:node:QueryDatabase [Error, Check dependency list! Synchronous require cannot resolve module 'https'. This is the first mention of this module!] {"scope":"bot-action","cardId":"card:Fetch Fixlet Data with Basic Authentication and HTTPS","nodeId":"node:QueryDatabase","workflowId":"flow:Main"}
@fresh-fireman-491 after removing the Https , I have this error Error fetching data: {} {"scope":"bot-action","cardId":"card:Fetch Fixlet Data with Basic Authentication","nodeId":"node:QueryDatabase","workflowId":"flow:Main"}
can you help me here @fresh-fireman-491
f
First thing
Where are you copying the errors from?
Second thing.
Can you make sure that it works outside of Botpress. You can try something like postman or Insomnia
f
I am copying the errors from Logs of the Botpress and yes I have checked the code and it runs and gets the response the error now I see is different https://cdn.discordapp.com/attachments/1230113925224337408/1230152155894255758/image.png?ex=66324779&is=661fd279&hm=a67b6ad4a792406e1c86dd6076f4ea2ae3a590af5aa772147a374bfb6740d7c0&
And thank you very much for looking into this and trying to help me
f
Alrighty. Can you get it to work from something like Insomnia or PostMan?
f
Dint try with Postman, but saved my code as .js and ran use node and file name it executed currently getting this in the logs Error fetching data: {} {"scope":"bot-action","cardId":"card:Fetch Fixlet data using Basic Authentication and a fixlet ID.","nodeId":"node:QueryDatabase","workflowId":"flow:Main"}
f
Can you update the error handling a bit and then run it again
Copy code
catch (error) {
    // Improved error logging
    console.error('Error fetching data:', error.response ? error.response.data : error.message);
  }
Also
Can you do 2 things. First is to send a screenshot of your code directly from Botpress. Next thing is to also send your code here, but use ` around the code so it doesn't get changed by the Discord markdown
Copy code
code here
This is the code "const data = 123456; async function fetchData() { try { const username = 'username'; // Replace with your username const password = 'password'; // Replace with your password const authString = Buffer.from(
${username}:${password}
).toString('base64'); const response = await axios.get(
https://10.134.130.27/api/extint/site/${data}
, { headers: { 'Authorization':
Basic ${authString}
// Add the Authorization header with the authString }, httpsAgent: new https.Agent({ rejectUnauthorized: false }) // Ignore SSL certificate validation (optional) }); console.log('Response data:', response.data); } catch (error) { // Improved error logging console.error('Error fetching data:', error.response ? error.response.data : error.message); } } fetchData();"
f
Hey
Thank you
f
"const data = 123456; async function fetchData() { try { const username = 'username'; // Replace with your username const password = 'password'; // Replace with your password const authString = Buffer.from(${username}:${password}).toString('base64'); const response = await axios.get(https://10.134.130.27/api/extint/site/${data}, { headers: { 'Authorization': Basic ${authString} // Add the Authorization header with the authString }, httpsAgent: new https.Agent({ rejectUnauthorized: false }) // Ignore SSL certificate validation (optional) }); console.log('Response data:', response.data); } catch (error) { // Improved error logging console.error('Error fetching data:', error.response ? error.response.data : error.message); } } fetchData();"
w
I'm pretty sure the error is coming from the
httpsAgent : https.Agent(/*...*/)
can you try just for fun remove the httpsAgent config in your axios request
f
I am getting a new error Error fetching data: Buffer is not defined {"scope":"bot-action","cardId":"card:Fetches data from a specific fixlet using Basic Authentication.","nodeId":"node:QueryDatabase","workflowId":"flow:Main"}
I removed the httpsagent
Hi @fresh-fireman-491 and @witty-football-93730 , It would be great if you can help me out here ,Thanks in advance
w
yeah that's what I thought. is there any reason you want to encode username and password as base64 ?
I can give you a piece of code that I know will work but I really need to know if this is absolutely necessary to do so
f
there is no reason , only thing is the API requires authetication , so using it
w
ok good so your API requires a basic auth scheme. let me paste a piece of code that will work bothon your studio and once your bot is deployed
w
Copy code
const username = 'hello'
const password = 'secret'

let authString = `${username}:${password}`
if(typeof Buffer !== 'undefined'){
     authString = Buffer.from(authString).toString('base64');
} else {
    authString = btoa(authString)
}

try {
 const response = await axios.get(`https://10.134.130.27/api/extint/site/${data}`, {
      headers: {
        'Authorization': `Basic ${authString}` // Add the Authorization header with the authString
      },
    });
    console.log('Response data:', response.data);
  } catch (error) {
    // Improved error logging
    console.error('Error fetching data:', error.response ? error.response.data : error.message);
  }
Not sure what is the `data`part of the url you are requesting but if you provide it it should work
The piece of code I just gave you will encode your authString as base64 and will work both in your studio and once the bot is deployed
if I have to see the response , how do I display it , today is the first day I am using botpress , so I hope I am not getting error from your code , but would like to know how to dislay the response for the get requst and also the response will be big
f
Does it show after waiting for a bit?
f
@fresh-fireman-491 it takes only a second and gives me this , does this mean , it works now ?
f
I suppose, depends on what you want with it
f
I want to get the response from the Get request
when I use the same code and save it as a script.js and run in my cmd with node script.js , I get the response for the Get request , but in here I dont , I tried a very basic script to provide the time and in the logs I could see time, but when I am using this code I only get api requested I beleive the code is getting execueted but not gathering the response @fresh-fireman-491 https://cdn.discordapp.com/attachments/1230113925224337408/1230438894592327780/image.png?ex=66335284&is=6620dd84&hm=d5bdf0a0128c2b1ef261f78ca9e6cec411ca32ad659f1abc6ddf68f734507936& https://cdn.discordapp.com/attachments/1230113925224337408/1230438895003631636/image.png?ex=66335285&is=6620dd85&hm=fcabbef96a17117aaaa144a5632a51ad9cf24f50f17ae75296866c15d535fdc1&
f
What do you mean gathering the response? Your screenshot had a response, right?
f
@fresh-fireman-491 , it does not display response , if you see my 2 screenshot the one which I use for time gather through api shows Time, but for my response there is only API CALL , the response is big xml which will return , I have 2 doubts either its a big xml hence its not returning the response or its not waiting for the response to complete
w
it all depends on what you do with the response in the code, do you store it in a workflow variable ?
If you don't do anything with the response data then it's normal that you don't see anything anywhere. In the piece of code I sent you there is a console.log statement, if it worked well then you should see the data showing in the logs. It's also possible that you get a timeout from the server you are making a request to. that would explain why there is no error and no data. I can't help you debug that as I have no access to the id address you sending a request. I recommnend you store the response data in a workflow variable and show that in a message card. This will help you understand if your request has completed or not
17 Views