I’ve been trying to use an API to get JSON data, but it requires you to add a token using an Authorization header.
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();