Tried making a fetch call to a URL to an API that requires basic authentication. I found a question that uses bases-64 module to encode the URL headers and put them in the fetch call. Tried variations but it doesn't work.
const fetch = require("node-fetch");
const base64 = require("base-64");
let url = "<URL>";
let username = "<username>";
let password = "<password>";
let headers = new Headers();
headers.set(
"Authorization",
"Basic " + base64.encode(username + ":" + password)
);
async function getCategories(url) {
fetch(url, {
method: "GET",
headers: headers,
})
.then((response) => {
return response.json;
})
.catch((err) => {
console.log(err);
});
}
getCategories(url)
.then((response) => console.log(response))
.catch((err) => console.log(err));
Error:
Headers is not defined
at Object.<anonymous> (C:\Users\User\Downloads\getCategories\getCategories.js:7:15)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
What am I doing wrong?