如何使用 node-fetch 查看发送的标头?

Posted

技术标签:

【中文标题】如何使用 node-fetch 查看发送的标头?【英文标题】:How to view the headers that were sent, using node-fetch? 【发布时间】:2020-09-25 11:51:19 【问题描述】:

我试图弄清楚如何访问节点获取请求发送的标头。当我这样做时,例如:

const response = await fetch('http://playground.localhost')

响应对象似乎只包含有关响应的信息(如响应标头、正文、url 等),没有任何“请求”对象,如 Axios 公开的。在后者中,您将能够通过 response.request.headers 访问标头。

有什么方法可以显示有关实际请求的信息,而不仅仅是响应?

【问题讨论】:

【参考方案1】:

这可能会有所帮助:https://www.npmjs.com/package/node-fetch#accessing-headers-and-other-meta-data

    .then(res => 
        console.log(res.ok);
        console.log(res.status);
        console.log(res.statusText);
        console.log(res.headers.raw());
        console.log(res.headers.get('content-type'));
    );```

【讨论】:

不,这是指响应头。我需要请求标头:D 对不起@i.brod,我只是更仔细地重新阅读了您的问题。希望其他人可以提供帮助。 好吧,我最终做的只是复制 fetch 发送的默认标头……但这当然不是“防弹” 感谢@Andy。我正在寻找一种获取响应标题的方法,您的回答帮助了我

以上是关于如何使用 node-fetch 查看发送的标头?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 node-fetch 通过 discord webhook 发送图像?

如何查看/调试手机浏览器发送的非标准请求标头?

向服务器发送请求时,如何查看 curl 生成的请求标头?

node-fetch 流文件到 nodemailer,然后 nodemailer 将文件作为附件发送

从画布正确获取信息并通过带有 node-fetch 的 webhook 将信息发送到 discord 时出现问题

在 node.js 中使用 node-fetch 将数据发送到 Pure Data (Pd) 导致 ETIMEDOUT