go - Parse cookie string in golang

ID : 274546

viewed : 35

Tags : cookiesgocookies





Top 3 Answer for go - Parse cookie string in golang

vote vote

97

A bit shorter version

package main  import (     "fmt"     "net/http" )  func main() {     rawCookies := "cookie1=value1;cookie2=value2"      header := http.Header{}     header.Add("Cookie", rawCookies)     request := http.Request{Header: header}      fmt.Println(request.Cookies()) // [cookie1=value1 cookie2=value2] } 

http://play.golang.org/p/PLVwT6Kzr9

vote vote

88

package main  import (     "bufio"     "fmt"     "net/http"     "strings" )  func main() {     rawCookies := "cookie1=value1;cookie2=value2"     rawRequest := fmt.Sprintf("GET / HTTP/1.0\r\nCookie: %s\r\n\r\n", rawCookies)      req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(rawRequest)))      if err == nil {         cookies := req.Cookies()         fmt.Println(cookies)     } } 

Playground

vote vote

70

Top 3 video Explaining go - Parse cookie string in golang







Related QUESTION?