【发布时间】:2022-08-14 02:42:18
【问题描述】:
在 Chrome 控制台中运行此代码时出现Unauthorized 错误(禁用网络安全并绕过 CSP),但它在 Node.js 和 Deno 和 Postman 中运行良好,返回预期的 JSON 对象。
Node.js 和 Deno 是否注入了一些标头以使其工作?
他们在做什么与浏览器的 fetch 不同?
我以为他们是一样的?
const profileId = \'foo\'; // not real values obviously
const sessionId = \'bar\';
getRelativesMetaData(profileId, sessionId).then(console.log);
async function getRelativesMetaData(myProfileId, sessionId) {
const headers = {
\'x-requested-with\': \'XMLHttpRequest\',
\'Cookie\': `current-profile-id=${myProfileId}; sessionid=${sessionId}`
};
const url = `https://you.23andme.com/p/${myProfileId}/family/relatives/ajax/`;
const resp = await fetch(url, { headers });
const json = await resp.json();
return json;
}
-
恰恰相反:
Cookie是forbidden header names 之一,任何兼容的浏览器都不允许您在请求中设置此标头。这个限制不适用于像 Deno 和 Node 这样的环境,因为它们不是浏览器,而且在那里没有意义。 -
此外,除非您在浏览器中从
you.23andme.com网页中运行此代码,否则也可能存在 COR 问题。
标签: node.js postman session-cookies fetch-api deno