gpt4 book ai didi

javascript - 有没有办法记录 Node 获取请求?

转载 作者:行者123 更新时间:2023-12-04 12:13:20 25 4
gpt4 key购买 nike

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

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 之上的。确实具有一些日志记录/调试功能的库。
它不会向您显示确切的传出请求,但会显示在构建最终 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 请求发送的确切数据的方法。您可能必须使用代理或网络记录器来查看发送到服务器的确切流。

关于javascript - 有没有办法记录 Node 获取请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66724333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com