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

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

Susan Sarandon
Susan SarandonOriginal
2024-11-24 10:48:10800browse

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) => { ...

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!

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