22

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
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Andrei Manolache
  • 766
  • 1
  • 8
  • 17
  • They have it in the documentation - https://www.npmjs.com/package/node-fetch#class-headers – Saurav Mar 20 '21 at 17:37
  • 1
    I want to see the final request, that is being sent to the server. I know how to set the headers myself, but I still want to see the **full final request** – Andrei Manolache Mar 20 '21 at 17:42

1 Answers1

52

The node-fetch library itself does not appear to have any debugging or logging ability built into it (by perusing the source on github). It is, however built on top of the http library which does have some logging/debugging capabilities.

It won't show you the exact outgoing request, but it will show you all the data is has gathered for the request right before it's constructed into the final http request. For example, if you set NODE_DEBUG=http in your environment before running your program, then you can get output like this:

HTTP 20624: call onSocket 0 0
HTTP 20624: createConnection google.com:80: {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'google.com',
  port: 80,
  hostname: 'google.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/',
  path: null,
  href: 'http://google.com/',
  method: 'GET',
  headers: [Object: null prototype] {
    'Some-Header': [ 'someValue' ],
    Accept: [ '*/*' ],
    'User-Agent': [ 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)' ],
    'Accept-Encoding': [ 'gzip,deflate' ],
    Connection: [ 'close' ]
  },
  agent: undefined,
  servername: 'google.com',
  _agentKey: 'google.com:80:'
}
HTTP 20624: sockets google.com:80: 1 1
HTTP 20624: outgoing message end.
(node:20624) Warning: Setting the NODE_DEBUG environment variable to 'http' can expose sensitive data (such as passwords, tokens and authentication headers) in the resulting log.
(Use `node --trace-warnings ...` to show where the warning was created)
HTTP 20624: requestTimeout timer moved to req
HTTP 20624: AGENT incoming response!
HTTP 20624: call onSocket 0 0
HTTP 20624: createConnection www.google.com:80: {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.google.com',
  port: 80,
  hostname: 'www.google.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/',
  path: null,
  href: 'http://www.google.com/',
  method: 'GET',
  headers: [Object: null prototype] {
    'Some-Header': [ 'someValue' ],
    Accept: [ '*/*' ],
    'User-Agent': [ 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)' ],
    'Accept-Encoding': [ 'gzip,deflate' ],
    Connection: [ 'close' ]
  },
  agent: undefined,
  servername: 'www.google.com',
  _agentKey: 'www.google.com:80:'
}
HTTP 20624: sockets www.google.com:80: 1 2
HTTP 20624: outgoing message end.
HTTP 20624: CLIENT socket onClose
HTTP 20624: removeSocket google.com:80: writable: false
HTTP 20624: HTTP socket close
HTTP 20624: requestTimeout timer moved to req
HTTP 20624: AGENT incoming response!
done
HTTP 20624: AGENT socket.destroySoon()
HTTP 20624: CLIENT socket onClose
HTTP 20624: removeSocket www.google.com:80: writable: false
HTTP 20624: HTTP socket close

And, if you set:

NODE_DEBUG=http,net,stream

You will get even more info. I still don't see any way with this debugging to get the exact data that is being sent out for the http request, though the data from the http module shows you what will be assembled into that request. You might have to use either a proxy or a network logger to see the exact stream being sent to the server.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Very cool, thank you for the info. I'll mark it as the accepted answer as there is no way to achieve what I asked and still managed to help. – Andrei Manolache Mar 20 '21 at 22:22