有没有办法记录节点获取请求?

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

所以我尝试使用 node-fetch 在 node.js 中使用一些 API,我想记录发送到服务器的最终请求,但我找不到任何方法。你能帮我吗?这是代码:

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

node-fetch 库本身似乎没有内置任何调试或日志记录功能(通过仔细阅读 github 上的 the source)。但是,它建立在 http 库之上,该库确实具有一些 logging/debugging 功能。

它不会向您显示确切的传出请求,但会向您显示在构造到最终 http 请求之前已为该请求收集的所有数据。例如,如果您在 运行 之前在您的环境中设置 NODE_DEBUG=http 您的程序,那么您可以获得如下输出:

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

并且,如果您设置:

NODE_DEBUG=http,net,stream

您将获得更多信息。尽管来自 http 模块的数据向您显示了将组装到该请求中的内容,但我仍然看不到任何通过此调试获取为 http 请求发送的确切数据的方法。您可能必须使用代理或网络记录器才能查看发送到服务器的确切流。