如何使用go语言发出请求获取cookie值并通过之前获取的cookie发出新请求
这里第一个post请求正在生成一个变量cookie,其形式为[sid=pcmpxxx+fidx1xxxx1cuk;路径=/;仅 http; samesite=严格]"
但我无法将此 cookie 发送到另一个发布请求(出现错误)。
带有注释的示例 go 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"log"
"strings"
"net/http"
"io/ioutil"
"net/http/cookiejar"
)
var client http.Client
func init() {
jar, err := cookiejar.New(nil)
if err != nil {
log.Fatalf("Got error while creating cookie jar %s", err.Error())
}
client = http.Client{
Jar: jar,
}
}
func main() {
urlx := "http://localhost:8080/api/v2/auth/login"
methodx := "POST"
payloadx := strings.NewReader(`username=admin&password=strongadmin`)
client := &http.Client {
}
reqx, err := http.NewRequest(methodx, urlx, payloadx)
if err != nil {
fmt.Println(err)
return
}
reqx.Header.Add("Referer", "http://localhost:8080/")
reqx.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
reqx.Header.Add("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
resx, err := client.Do(reqx)
if err != nil {
fmt.Println(err)
return
}
defer resx.Body.Close()
cookie := resx.Cookies()
fmt.Println(cookie)
bodyx, err := ioutil.ReadAll(resx.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(bodyx))
//second request start here
// this var cookies is a dummy cookie that i manually written,
// i need to replace the below variable 'cookies' to previously obtained variable 'cookie' without error
cookies := &http.Cookie{
Name: "SID",
Value: "PW16gD0Ja0OqixXeqpQle1WC/OK19tj+",
MaxAge: 300,
}
url := "http://localhost:8080/api/changes"
method := "POST"
payload := strings.NewReader(`json=%7B%data=valuex%22%3A%22300%22%7D`)
clientx := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
// req.Header.Add("Accept", "text/javascript, text/html, application/xml, text/xml, */*")
req.Header.Add("Referer", "http://localhost:8080/")
req.Header.Add("X-Requested-With", "XMLHttpRequest")
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
req.Header.Add("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
req.AddCookie(cookies)
res, err := clientx.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
//second request end here
}
正确答案
在第一个请求中,您获得的 cookie 为:
1
cookie := resx.cookies()
这是 cookie 结构 []*http.cookie 的切片
然后,如果您想在下一个请求中使用它们,请循环遍历它们并添加:
1
2
3
for _, c := range cookie {
req.AddCookie(c)
}
检查this example
终于介绍完啦!小伙伴们,这篇关于《如何使用 Go 语言发布请求获取 cookie 值并通过之前获取的 cookie 发布新请求?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~