简体   繁体   中英

Get cookie from URL

I am wondering if there is a way to get a cookie from a URL using golang? I have tried to use a few examples that come up when I google it but it never seems to return the cookie

This seems to do it:

package http
import "net/http"

func get_cookie(ref, name string) (*http.Cookie, error) {
   res, err := http.Get(ref)
   if err != nil {
      return nil, err
   }
   defer res.Body.Close()
   for _, cook := range res.Cookies() {
      if cook.Name == name {
         return cook, nil
      }
   }
   return nil, http.ErrNoCookie
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM