Ive attempted this below:
let options = {
hostname: `api.genius.com`,
path: `/${data}?text_format=plain`,
method: 'GET',
headers: {
'Authorization': `Bearer ${config.GeniusBearerToken}`,
'Host': `api.genius.com`
}
};
https.get(options, (res) => {
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('Request Failed. ' + `Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error('Invalid content-type.\n' + `Expected text/html; charset=UTF-8 but received ${contentType}`);
}
if (error) {
res.end(`<html> ERROR: ${e.message} </html>`)
console.error(error.message);
// Consume response data to free up memory
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
console.log(rawData)
} catch (e) {
res.end(`<html> ERROR: ${e.message} </html>`)
console.error(e.message);
}
}).on('error', (e) => {
res.end(`<html> ERROR: ${e.message} </html>`)
console.error(`Error: ${e.message}`);
});
});
As shown in my example everything works perfectly except im getting a 401 unauthorized code with the api, my config IS defined correctly and the config value is correct alongside with the header syntax, i just believe im doing it wrong.