简体   繁体   中英

Send cookie and API Key on the fetch request

I am sending a GET request to an API. The browser must send a cookie and an API Key on the fetch request .

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 :
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 :
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?

it's a Chrome problem,in some cases Chrome debugger do not show the cookie. The cookie has been sent i think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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