Fragment Description:



Your http.Cookie is specified as Secure.
'net.http.cookiejar.Jar' will therefore not return that cookie if you ask it about an http:// URL, even with a matching domain.
Changing the test URL to HTTPS causes the cookie to be returned !!

httpCookieJar

Last update, on 2015, Fri 9 Oct, 16:15:37

/* ... <== see fragment description ... */

package main

import (
    "fmt"
    "net/http"
    "net/http/cookiejar"
    "net/url"
    "time"
)

func main() {
    // 'net/http/cookiejar' stores cookies in memory
    jar, _ := cookiejar.New(nil)
    casurl, _ := url.Parse("https://httpbin.org")
    expire := time.Now().AddDate(0, 0, 10)
    /*sign: type Cookie {      type Cookie struct {
           Name  string
           Value string
           Path       string    // optional
           Domain     string    // optional
           Expires    time.Time // optional
           RawExpires string    // for reading cookies only
           // MaxAge=0 means no 'Max-Age' attribute specified.
           // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
           // MaxAge>0 means Max-Age attribute present and given in seconds
           MaxAge   int
           Secure   bool
           HttpOnly bool
           Raw      string
           Unparsed []string // Raw text of unparsed attribute-value pairs
       }
   */
    scookie := &http.Cookie{
        "something",
        "xxxxx",
        "/",
        "httpbin.org",
        expire,
        expire.Format(time.UnixDate),
        86400,
        true,
        true,
        "something=a",
        []string{"something=a"},
    }
    jar.SetCookies(casurl, []*http.Cookie{scookie})
    fmt.Println("--1--", jar)
    fmt.Println("--2--", jar.Cookies(casurl))
    c := &http.Client{Jar: jar}
    fmt.Println("--3--", jar.Cookies(casurl))
    fmt.Println("--4--", c.Jar)
}



Comments