I'm trying to use the new Fetch API and I'm having trouble handling cookies. Specifically, after a successful login, there is a Cookie header in future requests, but Fetch seems to ignore this header and all requests I make using Fetch are unauthorized.
Is this because Fetch is not ready yet, or does Fetch not support Cookies?
I use Webpack to build my application. I'm also using Fetch in React Native and don't have the same problem.
In addition to @Khanetor's answer, for those who are dealing with cross-origin requests, you can use
credentials: 'include'Example JSON fetch request:
fetch(url, { method: 'GET', credentials: 'include' }) .then((response) => response.json()) .then((json) => { console.log('Gotcha'); }).catch((err) => { console.log(err); });https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
By default, Fetch does not use cookies. To enable cookies,do the following:
fetch(url, { credentials: "same-origin" }).then(...).catch(...);