Why Doesn\'t My Browser Save Cookies from My Go Backend Unless I Specify `credentials: \'include\'`?

Susan Sarandon
Release: 2024-11-24 10:48:10
Original
726 people have browsed it

Why Doesn't My Browser Save Cookies from My Go Backend Unless I Specify `credentials:

Browser Fails to Preserve Cookie

In an attempt to construct a React application with a Go back-end, a cookie is set upon a request's response utilizing http.cookie. Despite this, the browser remains unable to store the cookie. This issue has been encountered in both Chrome and Firefox.

Upon closer inspection, it has been noticed that the 'credentials' attribute is absent from the Fetch API request expecting a response containing a cookie. By incorporating 'credentials: "include"' into the request, the browser is able to save the cookie acquired from the response.

fetch(`${url}/login`, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                credentials: "include", // Added parameter
                body: JSON.stringify({
                    email: userDetails.email,
                    password: userDetails.password,
                }),
            }).then((response) => { ...
Copy after login

This revelation highlights the significance of setting 'credentials' to "include" in requests expecting cookies in the response. The absence of this attribute prevents the browser from storing the cookie, resulting in the aforementioned issue.

The above is the detailed content of Why Doesn\'t My Browser Save Cookies from My Go Backend Unless I Specify `credentials: \'include\'`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template