And*_*che 3 javascript api fetch node.js node-fetch
所以我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)
该node-fetch库本身似乎没有内置任何调试或日志记录功能(通过仔细阅读github 上的源代码)。然而,它建立在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
Run Code Online (Sandbox Code Playgroud)
而且,如果您设置:
NODE_DEBUG=http,net,stream
Run Code Online (Sandbox Code Playgroud)
您将获得更多信息。尽管来自 http 模块的数据向您展示了将被组装到该请求中的内容,但我仍然没有看到通过此调试获得为 http 请求发送的确切数据的任何方法。您可能必须使用代理或网络记录器来查看发送到服务器的确切流。
| 归档时间: |
|
| 查看次数: |
1777 次 |
| 最近记录: |