import ( "net/http" "net/http/cookiejar" ) func main() { // Create a cookie jar to store cookies jar, err := cookiejar.New(nil) if err != nil { panic(err) } // Create a new HTTP client with the cookie jar client := &http.Client{ Jar: jar, } // Send a request with a cookie req, err := http.NewRequest("GET", "https://example.com", nil) if err != nil { panic(err) } // Set the "session_id" cookie with a value of "123" req.AddCookie(&http.Cookie{Name: "session_id", Value: "123"}) resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() // Handle the response }
res, err := http.Get("https://example.com") if err != nil { panic(err) } defer res.Body.Close() // Get the cookies set by the server cookies := res.Cookies() // Print the name and value of each cookie for _, cookie := range cookies { fmt.Printf("%s=%s\n", cookie.Name, cookie.Value) }This example sends a GET request to the example.com domain, and reads any cookies set by the server. It then prints the name and value of each cookie. The package library used in both examples is the net/http package.