简体   繁体   English

在 fetch 请求上发送 cookie 和 API Key

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

I am sending a GET request to an API.我正在向 API 发送 GET 请求。 The browser must send a cookie and an API Key on the fetch request .浏览器必须在获取请求上发送一个 cookie 和一个 API 密钥

The problem that I face is the following:我面临的问题如下:

  1. If I send the request without the API Key on header, the cookie is sent on the request :如果我在没有 header 上的 API 密钥的情况下发送请求,则会在请求中发送 cookie
fetch(urlToRequest, {
    method: "GET",
    mode: "cors",
    credentials: "include"
})
    .then((response) => {
        console.log(response);
    })
    .catch ((error) => {
        console.log(error)
    });
  1. If I include the Header with API Key, the cookie is not send on the request :如果我包含 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)
    });

Is this behave normal?这种行为正常吗? Shouldn't the browser send the cookie and the api key aswell?浏览器不应该发送 cookie 和 api 密钥吗?

it's a Chrome problem,in some cases Chrome debugger do not show the cookie.这是 Chrome 的问题,在某些情况下 Chrome 调试器不显示 cookie。 The cookie has been sent i think.我认为cookie已发送。

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

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