Error Network with GET request to World Time API
# 🤝help
b
Hi, today the bot is giving me this error when getting the dateTime from WorldTimeAPI, but the other day was working just fine and I didn't changed a single line of code . Can someone help me? EDIT: I noticed that the error pops up only in the emulator inside the studio. https://cdn.discordapp.com/attachments/1244574910081138716/1244574910232395776/image.png?ex=66559c35&is=66544ab5&hm=195bf57bf7c76081edb818315e241c3f049be1f96a3a0f9456d0bff6fcca0da0&
b
Are you still facing the same error?
f
I am also getting an error with this.
Copy code
javascript
async function getTimeInRome() {
    const timezone = 'Europe/Rome';
    const url = `http://worldtimeapi.org/api/timezone/${timezone}`;

    try {
        const response = await axios.get(url);
        const currentTime = response.data.datetime;
        console.log(`Current time in Rome: ${currentTime}`);
    } catch (error) {
        console.error('Error fetching time:', error);
    }
}

await getTimeInRome();
Error fetching time: {"message":"Network Error","name":"AxiosError","stack":"AxiosError: Network Error\n at l.onerror (https://studio.botpress.cloud/assets/vendor-76b6899.js:521:4465)\n at XMLHttpRequest.r (https://studio.botpress.cloud/assets/vendor-76b6899.js:503:1525)","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"adapter":["xhr","http"],"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"env":{},"headers":{"Accept":"application/json, text/plain, */*"},"withCredentials":false,"baseURL":"","method":"get","url":"https://botpress.studio/api/proxy/http%3A%2F%2Fworldtimeapi.org%2Fapi%2Ftimezone%2FEurope%2FRome"},"code":"ERR_NETWORK","status":null} I fixed it by not using their API.
Copy code
javascript
function getCurrentTime() {
  const now = new Date();
  
  now.setHours(now.getHours() + 2);
  
  const hours = now.getHours().toString().padStart(2, '0');
  const minutes = now.getMinutes().toString().padStart(2, '0');
  const seconds = now.getSeconds().toString().padStart(2, '0');
  
  const currentTime = `${hours}:${minutes}:${seconds}`;
  return currentTime;
}

console.log(getCurrentTime());
That runs without any issues and it outputs the current time for Rome.
p
I'd recommend using
Date.UTC()
to make sure that the current time won't be dependent on the timezone where the code is ran
f
Right of course, thank you!
b
Yes, and I don't know if it can be related, but when I tried to share the bot it gives me this message: 413 ERROR The request could not be satisfied. Bad request. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner. If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation. Generated by cloudfront (CloudFront) Request ID: 4dYOd81--_Ac9De6MsA5aSg31eJ2WmbHPIkvb7g_mKybnA1SdlE2Jw== But when I try to chat from the Sunshine Conversations integration it works just fine. https://cdn.discordapp.com/attachments/1244574910081138716/1244935148601081957/image.png?ex=6656ebb5&is=66559a35&hm=bb24c10e9336461e3c69a606faac1129525ca3d96b227eefe8111503082de789&
342 Views