I have a quite simple server application written in go:
package main
import (
"fmt"
"math/rand"
"time"
"net/http"
"encoding/base64"
"encoding/json"
"github.com/rs/cors"
)
type Message struct {
Text string `json:"text"`
}
var cookieQuotes = []string{
...
}
const COOKIE_NAME = "your_cookie"
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/set_cookie", setCookie)
mux.HandleFunc("/get_cookie", getCookie)
mux.Handle("/favicon.ico", http.NotFoundHandler())
handler := cors.Default().Handler(mux)
http.ListenAndServe(":8080", handler)
}
...
Well, I have skipped the most of code here, but let me explain what it does.
When we go to /set_cookie path, we are getting a cookie to our browser. Cookie has a Value, which is encrypted text of quote about a cookie. So, here is the function:
func setCookie(w http.ResponseWriter, r *http.Request) {
quote := getRandomCookieQuote()
encQuote := base64.StdEncoding.EncodeToString([]byte(quote))
http.SetCookie(w, &http.Cookie{
Name: COOKIE_NAME,
Value: encQuote,
})
}
This app is deployed in VirtualBox in a docker container.
So, when I'm accessing the
the cookie is set to the browser. And when I access
I get decrypted text from a cookie (but this doesn't matter in case of this question)
The client side is written in React. Well, I think it is not necessary to post all the code of the application, but the main part is here:
export const setCookie = () => async dispatch => {
const res = await axios.get(URL+SET_COOKIE_PATH);
console.log('set_cookie', res);
};
It is the the same thing as if we just called
But I can't get the cookie to the browser. Never. I get the response with status code 200 and everything seems to be okay, but I don't get a cookie.
I have to mention, that this is a cross site thing.
So, the question is: How could I pass cookie from server to client?