<\/span><\/h3>\n\n\n\ngo.mod<\/strong><\/p>\n\n\n\nmodule sample.com\/client\ngo 1.16<\/code><\/pre>\n\n\n\nclient.go<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"net\/http\"\n\t\"net\/http\/cookiejar\"\n\t\"net\/url\"\n)\n\nvar client http.Client\n\nfunc init() {\n\tjar, err := cookiejar.New(nil)\n\tif err != nil {\n\t\tlog.Fatalf(\"Got error while creating cookie jar %s\", err.Error())\n\t}\n\n\tclient = http.Client{\n\t\tJar: jar,\n\t}\n}\n\nfunc main() {\n\treq, err := http.NewRequest(\"GET\", \"http:\/\/localhost:8080\/doc\", nil)\n\tif err != nil {\n\t\tlog.Fatalf(\"Got error %s\", err.Error())\n\t}\n\tcookie := &http.Cookie{\n\t\tName: \"token\",\n\t\tValue: \"some_token\",\n\t\tMaxAge: 300,\n\t}\n\turlObj, _ := url.Parse(\"http:\/\/localhost:8080\/\")\n\tclient.Jar.SetCookies(urlObj, []*http.Cookie{cookie})\n\tresp, err := client.Do(req)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error occured. Error is: %s\", err.Error())\n\t}\n\tdefer resp.Body.Close()\n\n\tfmt.Printf(\"StatusCode: %d\\n\", resp.StatusCode)\n\n\treq, err = http.NewRequest(\"GET\", \"http:\/\/localhost:8080\/doc\/id\", nil)\n\tif err != nil {\n\t\tlog.Fatalf(\"Got error %s\", err.Error())\n\t}\n\n\tresp, err = client.Do(req)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error occured. Error is: %s\", err.Error())\n\t}\n\tdefer resp.Body.Close()\n\n\tfmt.Printf(\"StatusCode: %d\\n\", resp.StatusCode)\n}<\/code><\/pre>\n\n\n\nIn the above client program, we are creating an HTTP client with a cookie Jar<\/p>\n\n\n\n
jar, err := cookiejar.New(nil)\nif err != nil {\n log.Fatalf(\"Got error while creating cookie jar %s\", err.Error())\n}\n\nclient = http.Client{\n Jar: jar,\n}<\/code><\/pre>\n\n\n\nwe are adding a cookie to the Cookie Jar<\/p>\n\n\n\n
cookie := &http.Cookie{\n\t\tName: \"token\",\n\t\tValue: \"some_token\",\n\t\tMaxAge: 300,\n}\nurlObj, _ := url.Parse(\"http:\/\/localhost:8080\/\")\nclient.Jar.SetCookies(urlObj, []*http.Cookie{cookie})<\/code><\/pre>\n\n\n\nTo test the above code and to illustrate that cookies added to the cookie jar in the first call are indeed sent in the subsequent call as well, we need to create the server as well which will print the incoming cookies.<\/p>\n\n\n\n
<\/span>Server<\/strong><\/span><\/h3>\n\n\n\nThe server listens to port 8080 and has two APIs. These are the two APIs which is being hit by the client above.<\/p>\n\n\n\n
localhost:8080\/doc<\/strong><\/li>localhost:8080\/doc\/id<\/strong><\/li><\/ul>\n\n\n\nIn both the APIs we are printing the cookies which it receives in the incoming header<\/p>\n\n\n\n
go.mod<\/strong><\/p>\n\n\n\nmodule sample.com\/server\n\ngo 1.16<\/code><\/pre>\n\n\n\nserver.go<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net\/http\"\n)\n\nfunc main() {\n\tdocHandler := http.HandlerFunc(docHandler)\n\thttp.Handle(\"\/doc\", docHandler)\n\n\tdocGetID := http.HandlerFunc(docGetID)\n\thttp.Handle(\"\/doc\/id\", docGetID)\n\n\thttp.ListenAndServe(\":8080\", nil)\n}\n\nfunc docHandler(w http.ResponseWriter, r *http.Request) {\n\tfmt.Println(\"Cookie in First API Call\")\n\tfor _, c := range r.Cookies() {\n\t\tfmt.Println(c)\n\t}\n\tfmt.Println()\n\tw.WriteHeader(200)\n\tw.Write([]byte(\"Doc Get Successful\"))\n\treturn\n}\n\nfunc docGetID(w http.ResponseWriter, r *http.Request) {\n\tfmt.Println(\"Cookie in Second API Call\")\n\tfor _, c := range r.Cookies() {\n\t\tfmt.Println(c)\n\t}\n\tw.WriteHeader(200)\n\tw.Write([]byte(\"Doc Get ID Successful\"))\n\treturn\n}<\/code><\/pre>\n\n\n\nNow run the server<\/p>\n\n\n\n
go run server.go<\/code><\/pre>\n\n\n\nand client<\/p>\n\n\n\n
go run client.go<\/code><\/pre>\n\n\n\nNotice the output at the server end<\/p>\n\n\n\n
Cookie in First API Call\ntoken=some_token\n\nCookie in Second API Call\ntoken=some_token<\/code><\/pre>\n\n\n\nThe same cookie is being sent automatically\u00a0in both the first and second call which the client will make to the server. How does it work out\u00a0of the box? This is because CookieJar<\/strong> comes into the picture. The golang HTTP client checks the Cookie Jar before making the HTTP call. It then sends this cookie.<\/p>\n\n\n\nThis is all about setting cookies in golang. Hope you have liked the tutorial. Please share feedback in the comments.<\/p>\n\n\n\n
Also, check out our Golang advance tutorial Series – Golang Advance Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewSet cookie for one of the requestSet cookie and send that cookie for all requests to that domainClientServer Overview A cookie in golang is represented as below in golang…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[316,3,4,101],"class_list":["post-5532","post","type-post","status-publish","format-standard","hentry","category-tech","tag-cookie","tag-go","tag-golang","tag-set"],"yoast_head":"\n
Set cookie http in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n