title: Response redirect to other domain with a cookie in golang
body: <p>I try to set a cookie in one domain and send it to another domain.</p>
  
  <p>Architecture :</p>
  
  <pre><code>API-Gateway -&gt; 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.
  
  </code></pre>
  
  <pre><code>    expiration := time.Now().Add(24 * time.Hour)
      cookie    := http.Cookie{Name: "usercookie",Value:jwtToken,Expires:expiration,Path:"/"}
      http.SetCookie(res, &amp;cookie)
  
      redirectURL := "http://localhost:7070/home?usercookie=" + url.QueryEscape(cookie)
  
      http.Redirect(res, req, redirectURL , http.StatusFound)
  </code></pre>
  
  <p>I get </p>
  
  <blockquote>
    <p>cannot use cookie (type http.Cookie) as type string in argument to url.QueryEscape</p>
  </blockquote>
  
  <p>How can I overcome this issue?
  My systems are running as micro-services, so how can I solve this issue?</p>
  
  <p>Is there other options to use cookies over domains?</p>
  
  <p>Edit :</p>
  
  <p>When I use just <code>http.Cookie()</code> and then redirect flow from user service.</p>
  
  <pre><code>// User service
      expiration := time.Now().Add(24 * time.Hour)
      cookie    := http.Cookie{Name: "usercookie",Value:jwtToken,Expires:expiration}
      http.SetCookie(res, &amp;cookie)
  
      http.Redirect(res, req, "http://localhost:7070/home" , http.StatusFound)
  </code></pre>
  
  <pre><code>// 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)
  
  }
  </code></pre>
  
  <p>Output : </p>
  
  <blockquote>
    <p>There is an error in cookie :  http: named cookie not present</p>
  </blockquote>
  
  <p><a href="https://www.reddit.com/r/golang/comments/ch7a4h/how_to_send_a_cookie_or_a_header_through/" rel="nofollow noreferrer">How to send a cookie or a header through http.Redirect</a></p>
  
  <p><a href="https://stackoverflow.com/questions/46063816/http-redirect-302-doesnt-use-cookie-in-following-get-request">HTTP Redirect (302) Doesn't Use Cookie in Following GET Request</a></p>