cookiesgo

Parse cookie string in golang


If I get the cookie by typing document.cookie in the browser, is there any way to parse the raw string and save it as a http.Cookie?


Solution

  • 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