I am trying to test if I am in domain A, can domain A client send domain B cookie to domain B.
Here is my golang code
package main
import (
"fmt"
"net/http"
"log"
"time"
"encoding/json"
)
func setCookie(w http.ResponseWriter, r *http.Request) {
expiration := time.Now().Add(365 * 24 * time.Hour)
cookie := http.Cookie{Path: "/test_receive_cookie", Name: "test_cors", Value: "test_cors", Expires: expiration}
http.SetCookie(w, &cookie)
fmt.Fprintf(w, "Success")
}
func receiveCookie(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Cookies())
w.Header().Set("Access-Control-Allow-Origin", "*")
data := make(map[string]interface{})
for _, cookie := range r.Cookies() {
data[cookie.Name] = cookie.Value
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
func main() {
http.HandleFunc("/set_cookie", setCookie)
http.HandleFunc("/test_receive_cookie", receiveCookie)
err := http.ListenAndServe(":8012", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
I first hit http://localhost:8012/set_cookie
, then I open a html file containing a javascript using this library
this._xhr.get(
"http://localhost:8012/test_receive_cookie",
function(err, resp) {
console.log(resp);
console.log(err);
});
Here is result of fmt.Println(r)
&{GET /test_receive_cookie HTTP/1.1 1 1 map[Accept:[*/*] Accept-Encoding:[gzip, deflate, br] Accept-Language:[en-US,en;q=0.9] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36] Connection:[keep-alive] Pragma:[no-cache] Cache-Control:[no-cache] Origin:[null]] {} <nil> 0 [] false localhost:8012 map[] map[] <nil> map[] [::1]:61817 /test_receive_cookie <nil> <nil> <nil> 0xc420014600}
The following happened
My server print
[]
fromfmt.Println(r.Cookies())
If I hit
http://localhost:8012/test_receive_cookie
I can see the cookie I set gets print out on browser, but when I open a html that calls the endpoint the server will have empty cookie
My questions is how can I pass the cookie set by http://localhost:8012/test_receive_cookie
back to http://localhost:8012/test_receive_cookie
using client code?
Am I missing some configuration code?
