繁体   English   中英

在 fetch 请求上发送 cookie 和 API Key

[英]Send cookie and API Key on the fetch request

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

我面临的问题如下:

  1. 如果我在没有 header 上的 API 密钥的情况下发送请求,则会在请求中发送 cookie
fetch(urlToRequest, {
    method: "GET",
    mode: "cors",
    credentials: "include"
})
    .then((response) => {
        console.log(response);
    })
    .catch ((error) => {
        console.log(error)
    });
  1. 如果我包含 Header 和 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 密钥吗?

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

暂无
暂无

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

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