I try to set a cookie in one domain and send it to another domain.
Architecture :
API-Gateway -> User service
When a login request receives to Gateway, it forwards it to User service.
User service does credential checking, if it matches, then redirect it to gateway.
expiration := time.Now().Add(24 * time.Hour)
cookie := http.Cookie{Name: "usercookie",Value:jwtToken,Expires:expiration,Path:"/"}
http.SetCookie(res, &cookie)
redirectURL := "http://localhost:7070/home?usercookie=" + url.QueryEscape(cookie)
http.Redirect(res, req, redirectURL , http.StatusFound)
I get
cannot use cookie (type http.Cookie) as type string in argument to url.QueryEscape
How can I overcome this issue? My systems are running as micro-services, so how can I solve this issue?
Is there other options to use cookies over domains?
Edit :
When I use just http.Cookie()
and then redirect flow from user service.
// User service
expiration := time.Now().Add(24 * time.Hour)
cookie := http.Cookie{Name: "usercookie",Value:jwtToken,Expires:expiration}
http.SetCookie(res, &cookie)
http.Redirect(res, req, "http://localhost:7070/home" , http.StatusFound)
// Gateway home controler
func home(res http.ResponseWriter,req *http.Request){
cookie,cookieError:= req.Cookie("usercookie")
if cookieError != nil{
log.Println("There is an error in cookie : ",cookieError)
}
log.Println("User cookie is : ",cookie.Name)
}
Output :
There is an error in cookie : http: named cookie not present
How to send a cookie or a header through http.Redirect
HTTP Redirect (302) Doesn't Use Cookie in Following GET Request