首页 > 解决方案 > 如何在 Go 中使用 cookie jar 维护相同的会话

问题描述

我想知道如何在 Go 中使用 cookie jar 来维护相同的会话

我使用这种方法在 JS 中做到了这一点:

const cookieJar = request.jar();

request({
    headers: {
    //headers here
    },
    url: url,
    jar: cookieJar
    method: 'GET'

我想知道 Go 是否有上述代码的等价物。谢谢!

标签: gorequestsession-cookiesgo-http

解决方案


你猫尝试这样的事情:

req, err := http.NewRequest("GET", "http://localhost:8080/", nil)
if err != nil { // ... }

jar, err := cookiejar.New(nil)
if err != nil { // ... }

client := &http.Client{Jar: jar}

res, err := client.Do(req)
if err != nil { // ... }

推荐阅读