Hi!
I am trying to call some external APIs from JS server code.
Initially I tried using XMLHttpRequest but I get the error: “XMLHttpRequest is not defined”;
So instead I am using this:
class myClass {
/**
* @param {String} id
*/
async doThis(id){
var token = "myToken";
var https = require('https');
var json = {'payload':'some payload'};
var options = {
hostname: 'domain.com', port: 443,
path: '/path', method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token,}
};
var req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
console.log(d);
});
});
req.on('error', (e) => {
console.log(e);
});
req.write(json);
req.end();
}
However this doesn’t seem to post anything as far as I can see and no return.
What is the preferred way to call an external API from server code?
Thanks
Tony