Saturday, 19 September 2020

Node.js GET error with discord.js when fetching data from atomicassets (wax.atomichub.io)

I'm trying to get a bot to post a message in discord listing some JSON requested from AtomicAssets. With curl it works fine and I get JSON back (curl -X GET "https://test.wax.api.atomicassets.io/atomicassets/v1/assets?page=1&limit=100&order=asc&sort=asset_id" -H "accept: application/json")

The problem is that when I run my bot and tell it to https GET the JSON, I get this error message:

Error: getaddrinfo ENOTFOUND https://test.wax.api.atomicassets.io/atomicassets
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'https://test.wax.api.atomicassets.io/atomicassets'
}

Why can't it just do the same as curl?

Here's a minimal working example of my bot.js file:

// stores API keys in separate JSON file
const { token } = require('./token.json');

// require the discord.js module
const Discord = require('discord.js');

// create a new Discord client
const bot = new Discord.Client();

// auth
bot.login(token);

bot.on('message', message => {

    // fetch AtomicAssets items
    else if (message.content === 'assets') {
        const https = require('https')
        const options = {
            hostname: 'https://test.wax.api.atomicassets.io/atomicassets',
            path: '/v1/assets?page=1&limit=100&order=asc&sort=asset_id',
            method: 'GET'
        }

        const req = https.request(options, res => {
            console.log(`statusCode: ${res.statusCode}`)

            res.on('data', d => {
                process.stdout.write(d)
                return message.reply(d)
            })
        })

        req.on('error', error => {
            console.error(error)
        })

        req.end()
    }
});

I've also tried to use AtomicAssets' npm package, but to no avail.

Help is much appreciated!



from Node.js GET error with discord.js when fetching data from atomicassets (wax.atomichub.io)

No comments:

Post a Comment