有没有办法记录节点获取请求?
Posted
技术标签:
【中文标题】有没有办法记录节点获取请求?【英文标题】:Is there a way to log the node-fetch request? 【发布时间】:2021-06-17 19:35:06 【问题描述】:所以我尝试使用 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
【问题讨论】:
他们在文档中有 - npmjs.com/package/node-fetch#class-headers 我想查看正在发送到服务器的最终请求。我知道如何自己设置标头,但我仍然想查看完整的最终请求 【参考方案1】:node-fetch
库本身似乎没有内置任何调试或日志记录功能(通过仔细阅读 github 上的 the source)。然而,它是建立在 http
库之上的,该库确实具有一些日志记录/调试功能。
它不会向您显示确切的传出请求,但会显示在构建最终 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 请求发送的确切数据的方法。您可能必须使用代理或网络记录器才能查看发送到服务器的确切流。
【讨论】:
非常酷,感谢您提供的信息。我会将其标记为已接受的答案,因为无法实现我所要求的并且仍然设法提供帮助。以上是关于有没有办法记录节点获取请求?的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法使用 HealthKit 在 iOS 上请求对非临床和临床记录的授权?
有没有办法获取当前接受的 Lyft 乘车请求信息以及乘客信息?