go - 如何在 Go 中使用 cookie jar 维护相同的会话
问题描述
我想知道如何在 Go 中使用 cookie jar 来维护相同的会话
我使用这种方法在 JS 中做到了这一点:
const cookieJar = request.jar();
request({
headers: {
//headers here
},
url: url,
jar: cookieJar
method: 'GET'
我想知道 Go 是否有上述代码的等价物。谢谢!
解决方案
你猫尝试这样的事情:
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 { // ... }
推荐阅读
- java - 如何编写读取数组列表中特定内容并计算数组内容数量的方法
- android - 为什么 Google Ads 只会在发布版本上崩溃 Android 应用?
- visual-studio-code - 我们如何在 VSCode 编辑器中做 Emacs 之类的缩进?
- c++ - 如何提高与 std::vector 共享内存的数据映射(Eigen::Map)矩阵的 GEMM 性能?
- java - 在异常消息设置中显示代码详细信息
- java - 无法使用 Junits 覆盖 Catch 块
- ros - 使用 hector_slam 进行本地化
- laravel - Laravel 雄辩关系中的“关键”
- indexing - 为什么在递归调用中索引字符串会产生不同的结果?
- algorithm - 对 ⌈logn⌉ 已排序的 ⌊n/logn⌋ 元素列表中的所有元素进行排序的时间复杂度是多少?