【发布时间】: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
【问题讨论】:
-
我想查看正在发送到服务器的最终请求。我知道如何自己设置标头,但我仍然想查看完整的最终请求
标签: javascript node.js api fetch node-fetch