Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
Is there a way to log the node-fetch request?

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
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 gather 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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!