I am trying to get data from an external api, which requires basic authentication. I am using node + express, and the https module.
I am not getting any errors, simply a 401 response status code, I just don't get to log in.
Also, on postman I do get the response without any problems. Which means that both the url and the authentication data I am passing are correct.
Really stuck here. Any help will be massively appreciated!
Manel
My code:
const express = require('express');
const https = require("https");
const basic = require('basic-authorization-header');
const app = express();
const auth = {
'Authorization': basic("username", "password"),
};
app.get("/", (req, res) => {
const url = "https://dictapi.lexicala.com/search?source=global&language=en&text=working&analyzed=true"
https.get(url, auth, (response) => {
console.log(response.statusCode);
console.log(auth);
response.responseType="text";
response.on("data", (data)=> {
const translationLot = JSON.parse(data)
console.log(translationLot);
})
})
res.send("Server is up and running")
});
const PORT = process.env.PORT || 7070;
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
Output on terminal:
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Server is running on port 7070
401
{ Authorization: 'Basic TW5M2FzOMQ==' }
{ status: 401, message: 'Not logged in' }