Search code examples
javascriptdomcookiesbasic-authenticationfetch-api

How do I use window.fetch() with httpOnly cookies or basic auth


I'm playing around with window.fetch() in Firefox and Chrome. For some reasons, fetch() doesn't send any cookies. Now that wouldn't be a problem, as I can send them using

fetch('/something', { headers: { Cookie: document.cookie } })

But this won't work for httpOnly cookies.


Solution

  • Okay, I found out after reading on the Mozilla Developer Network a bit more and trying out the credentials option.

    Looks like the credentials option is what I should have looked for.

    fetch('/something', { credentials: 'same-origin' }) // or 'include'
    

    Will send the cookies.