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

夙愿已清 提交于 2019-12-24 07:14:35

问题


I'm trying to intercept Set-Cookie response header from fetch's response:

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]}`);
    }
});

But not all of the headers (as can be seen in developer tools' network) can be found in there! Why is that? Is there any way I can get the header I'm looking for?

Just for clarification, I'm not looking for the cookie but I'm interested to know when the Set-Cookie header is sent.


回答1:


You cannot read the Set-Cookie header as it is declared as forbidden. The fetch polyfill on github provides a reasonable explanation:

Like with XMLHttpRequest, the Set-Cookie response header returned from the server is a forbidden header name and therefore can't be programatically read with response.headers.get(). Instead, it's the browser's responsibility to handle new cookies being set (if applicable to the current URL). Unless they are HTTP-only, new cookies will be available through document.cookie.

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



来源:https://stackoverflow.com/questions/39672318/why-not-all-headers-can-be-found-in-fetchs-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!