为什么不是所有的头都能在fetch'的响应中找到

Why not all headers can be found in fetch's response?

本文关键字:响应 fetch 为什么不      更新时间:2023-09-26

我正试图拦截Set-Cookie响应头从fetch的响应:

fetch('https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png', {
	method: 'get'
}).then(function(response) {
    for (var pair of response.headers.entries()) {
        console.log(`${pair[0]}: ${pair[1]}`);
    }
});

但不是所有的头(可以在开发人员工具的网络中看到)可以在那里找到!为什么呢?有什么方法可以让我得到我想要的标题吗?

只是为了澄清,我不是在寻找cookie,但我有兴趣知道Set-Cookie头何时发送。

您不能读取Set-Cookie头,因为它被声明为禁止的。github上的fetch填充提供了一个合理的解释:

与XMLHttpRequest一样,Set-Cookie响应头从服务器是被禁止的标头名称,因此不能用response.headers.get()编程读取。相反,它是浏览器处理正在设置的新cookie的责任(如果适用于当前URL)。除非它们是仅限http的新cookie将通过document.cookie.

https://github.com/github/fetch receiving-cookies