Golang : Storing cookies in http.CookieJar example
Cookie can be useful for storing data and to be retrieved later on to fill in form values such as username, address or other hidden parameters that a server might need. For this tutorial, we will explore how to store values into a cookies jar and when the client(usually a browser) encounter the URL the cookies are associated with, the values will be utilized back.
The code below simulate an event where the user visited back youtube.com and the cookies values are reused back.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
)
func main() {
jar, _ := cookiejar.New(nil)
var cookies []*http.Cookie
firstCookie := &http.Cookie{
Name: "PREF",
Value: "f1=50000000&f5=30",
Path: "/",
Domain: ".youtube.com",
}
cookies = append(cookies, firstCookie)
secondCookie := &http.Cookie{
Name: "VISITOR_INFO1_LIVE",
Value: "Luy_gz784rQ",
Path: "/",
Domain: ".youtube.com",
}
cookies = append(cookies, secondCookie)
thirdCookie := &http.Cookie{
Name: "YSC",
Value: "9jGyTn_JiBk",
Path: "/",
Domain: ".youtube.com",
}
cookies = append(cookies, thirdCookie)
fourthCookie := &http.Cookie{
Name: "dkv",
Value: "19a17a80fd703efd450d5ef9dadc32cee3QEAAAAdGxpcGn9mTBVMA==",
Path: "/",
Domain: ".youtube.com",
}
cookies = append(cookies, fourthCookie)
// URL for cookies to remember. i.e reply when encounter this URL
cookieURL, _ := url.Parse("https://www.youtube.com/results?search_query=")
jar.SetCookies(cookieURL, cookies)
// sanity check
fmt.Println(jar.Cookies(cookieURL))
//setup our client based on the cookies data
client := &http.Client{
Jar: jar,
}
urlData := url.Values{}
urlData.Set("search_query", "macross")
req, _ := http.NewRequest("POST", "https://www.youtube.com/results?search_query=", strings.NewReader(urlData.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
panic(nil)
}
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
// display content to screen ... save this to a HTML file and view the file with browser ;-)
fmt.Println(string(body))
}
NOTE : If you are using Google Chrome browser, go to Views->Developer->Developer Tools and under the Resources tab, you should see Cookies->www.youtube.com. The cookies values use in the example above are taken from there.
Reference :
See also : Golang : Drop cookie to visitor's browser and http.SetCookie() example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.6k Mac OSX : Find large files by size
+6.5k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+9.4k Golang : Quadratic example
+18.9k Golang : Get RGBA values of each image pixel
+8.5k Golang : Accept any number of function arguments with three dots(...)
+5.2k How to check with curl if my website or the asset is gzipped ?
+13.6k Golang : Compress and decompress file with compress/flate example
+19.6k Golang : How to run your code only once with sync.Once object
+9.3k Golang : Read file with ioutil
+8.5k Yum Error: no such table: packages
+15.2k Golang : How to convert(cast) IP address to string?
+10.6k Golang : Natural string sorting example