Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 555
Andrei Manolache Avatar asked Mar 20 '21 17:03

Andrei Manolache


People also ask

How do I get data from a fetch response?

Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.

Can you use fetch in node?

Fetch is already available as an experimental feature in Node v17. If you're interested in trying it out before the main release, you'll need to first download and upgrade your Node. js version to 17.5.

Can fetch be used in Express?

You might have to request the public REST APIs on the server. Since fetch is a web API, you will not be able to use it on server code. Although there is an npm package that can help you utilize the capabilities of fetch while writing Express code. It's called node-fetch .

How do I make a node-fetch POST request?

The first thing we do is require() the Node-Fetch NPM package and assign it a variable name of fetch to use in our application. Then we create an object called body that holds the data we will include in our POST request. This includes the title , body , and userId items.


1 Answers

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.

like image 98
jfriend00 Avatar answered Sep 27 '22 23:09

jfriend00