【问题标题】:Send cookie and API Key on the fetch request在 fetch 请求上发送 cookie 和 API Key
【发布时间】:2020-06-15 17:05:09
【问题描述】:

我正在向 API 发送 GET 请求。 浏览器必须在获取请求时发送一​​个 cookie 和一个 API 密钥

我面临的问题如下:

  1. 如果我在标头上没有 API 密钥的情况下发送请求,cookie 会在请求中发送
fetch(urlToRequest, {
    method: "GET",
    mode: "cors",
    credentials: "include"
})
    .then((response) => {
        console.log(response);
    })
    .catch ((error) => {
        console.log(error)
    });
  1. 如果我包含带有 API 密钥的标头,cookie 不会在请求中发送
fetch(urlToRequest, {
    method: "GET",
    mode: "cors",
    credentials: "include",
    headers: {"api-key": apiKey}
})
    .then((response) => {
        console.log(response);
    })
    .catch ((error) => {
        console.log(error)
    });

这种行为正常吗?浏览器不应该同时发送cookie和api密钥吗?

【问题讨论】:

    标签: javascript api cookies fetch


    【解决方案1】:

    这是 Chrome 的问题,在某些情况下 Chrome 调试器不显示 cookie。我认为 cookie 已发送。

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 2019-12-24
      • 2015-06-22
      • 2019-02-02
      • 2020-11-24
      • 2020-07-29
      • 2016-06-03
      • 2021-10-30
      • 2017-01-27
      相关资源
      最近更新 更多