So I'm trying to use some APIs in node.js using node-fetch and I would like to log the final request that is being sent to the server, but I can't find any way how to do it. Can you help me please? Here is the code:
const fs = require('fs');
const fetch = require('node-fetch');
const https = require('https');
const reqUrl = 'https://endpoint.com';
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Digest': 'SHA-256=47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=',
'Date': 'Sat, 20 Mar 2021 15:42:18 GMT',
'X-Request-ID': 'request_id',
'Authorization': 'Bearer my_bearer',
'Signature': 'my_signature'
};
const certs = {
key: fs.readFileSync('path_to_key'),
cert: fs.readFileSync('path_to_cert')
};
async function getAccounts() {
const options = {
cert: certs.cert,
key: certs.key,
rejectUnauthorized: false
};
const sslConfiguredAgent = new https.Agent(options);
try {
// here is the problem. How to view the final request header?
fetch(reqUrl, {
method: 'GET',
headers: headers,
agent: sslConfiguredAgent
}).then(response => {
const headers = response.headers;
console.log(headers); // I know that this log outputs the RESPONSE headers, I want to find out how to output the REQUEST headers
});
} catch (error) {
console.log(error);
}
};
getAccounts(); // function call