How to make an NodeJS HTTP request to the RobotEvents API

I tried looking directly at the docs: https://nodejs.org/en/knowledge/HTTP/clients/how-to-create-a-HTTP-request/, but I followed it directly and it didn’t work.

Here is my current code:

const http = require('http')
require('dotenv').config()

const token = process.env.ROBOTEVENTS_TOKEN
const host = 'www.robotevents.com', path = '/api/v2/events?sku%5B%5D=RE-VRC-21-5414&myEvents=false'

var options = {
  host: host,
  path: path,
  headers: {
    Authorization: `Bearer: ${token}`,
    accept: 'application/json'
  }
};

callback = function(response) {
  var str = ''

  response.on('data', function (chunk) {
    str += chunk;
    console.log('chunk')
  });

  response.on('end', function () {
    console.log(str);
  });
}

var req = http.request(options, callback);
req.end();

You would likely want to make use of the fetch API, as this makes consuming JSON significantly easier in Node.js.

Alternatively, consider using the robotevents module I wrote a little while back, which should hopefully abstract over a lot of the API

3 Likes

Are you talking about this Fetch API: node-fetch - npm?
Also, how would you include headers in the Fetch API request?