Intro: I have and angular frontend and node backend (Server A) working fine. Using nginx as well for Server A. Now from my node server (Server A) I need to call an API endpoint of other server (Server B).
Nginx configurations for Server B wont matter because curl command is working fine.
I have a PUT and POST https outgoing request from my node server to other server. On my system I have nginx service working.
The API is working fine when I use curl from the terminal but for the same thing via node https module the server is giving 400 BAD REQUEST.
- What is the process for outgoing requests for nginx?
- Why is the curl command working and node env requests not working?
curl -i --insecure --request PUT --url https://example.com:443/update --header 'content-type:application/json' --data '{"example":"Goodmorning"}'
httpsRequest(serverurl, method, header, data, callback){
console.log("httpsRequest function body")
let datastr = "";
if (data !== undefined)
datastr = JSON.stringify(data);
const options = {
host:'example.com',
port : 443,
rejectUnauthorized: false,
path : "/update",
body : datastr,
method : method,
secure:false,
key : readFileSync("example.key"),
cert : readFileSync("example.crt"),
};
if (header !== undefined) {
options['headers'] = header
};
}
console.log("options\n", options);
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
options.agent = new https.Agent(options);
// options.agent = httpsAgent;
const req = https.request(options, (res) => {
console.log('status code ', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
console.log(d);
if(res.statusCode === "errorexample"){
callback(null, {data : "success"})
}else{
let errormsg = {
message : res.message,
statusCode : res.statusCode,
failureAlarmCode : res.failureAlarmCode
}
callback(null, { "error": errormsg });
}
});
});
req.on('error', (e) => {
console.error(e);
callback(e, null);
});
req.end();
}
I think the curl from terminal does not route via the client nginx, hence some difference there. AM I correct?