繁体   English   中英

fetch 适用于 Node.js 和 Deno 但不适用于浏览器或 Postman

[英]fetch works in Node.js and Deno but not in browser or Postman

I get an Unauthorized error when running this code in the Chrome console (with web security disabled and CSP bypass), but it works fine in Node.js and Deno and Postman, returning the expected JSON object.

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是被禁止的 header 名称之一,任何兼容的浏览器都不允许您在请求中设置此 header。 此限制不适用于 Deno 和 Node 等环境,因为它们不是浏览器,因此在那里没有意义。


Perhaps you are aware that 23andMe does have an API — but it's restricted-access, which (I guess) is why you are wanting to simply use your session cookie to access your data (presumably in a browser-based web app that you've创建)。 如果我对此的直觉是正确的,那么您可能会从使用 Deno/Node/etc 操作代理服务器中受益。 这样您就可以在浏览器应用程序中获取数据,而无需担心该环境中的身份验证限制(这也提供了不在客户端应用程序代码中公开 session 信息的好处)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM