Response redirect to other domain with a cookie in golang

烈酒焚心 提交于 2020-04-18 05:48:29

问题


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

来源:https://stackoverflow.com/questions/61016521/response-redirect-to-other-domain-with-a-cookie-in-golang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!