/
exp_grequests.go
198 lines (172 loc) · 4.87 KB
/
exp_grequests.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"encoding/json"
"fmt"
"github.com/levigross/grequests"
"log"
)
func getSession() {
session := grequests.NewSession(nil)
resp, err := session.Get(
"http://httpbin.org/cookies/set",
&grequests.RequestOptions{Params: map[string]string{"one": "two"}},
)
if err != nil {
log.Fatal("Cannot set cookie: ", err)
}
if resp.Ok != true {
log.Println("Request did not return OK")
}
fmt.Println(resp.String())
}
func get() {
ro := &grequests.RequestOptions{Params: map[string]string{"One": "Two"}}
resp, err := grequests.Get("http://httpbin.org/get", ro)
if err != nil {
log.Fatalln("Unable to make request: ", err)
}
// 打印返回内容
fmt.Println(resp.String())
type GetResponse struct {
Args struct{} `json:"args"`
Headers struct {
Accept string `json:"Accept"`
AcceptEncoding string `json:"Accept-Encoding"`
AcceptLanguage string `json:"Accept-Language"`
Dnt string `json:"Dst"`
Host string `json:"Host"`
UserAgent string `json:"User-Agent"`
Authorization string `json:"Authorization"`
} `json:"headers"`
Origin string `json:"origin"`
URL string `json:"url"`
}
getResponse := &GetResponse{}
if err := resp.JSON(getResponse); err != nil {
fmt.Println(err.Error())
}
// 打印构造结构
fmt.Println(getResponse)
fmt.Println(getResponse.URL)
// 打印转换内容 - Json
jsonBytes, errJ := json.Marshal(getResponse) //转换成JSON返回的是byte[]
if errJ != nil {
fmt.Println(errJ.Error())
}
fmt.Println(string(jsonBytes))
// 打印转换内容 - Json 缩进
jsonIndentBytes, errJI := json.MarshalIndent(getResponse, "", "\t") //转换成JSON返回的是byte[]
if errJI != nil {
fmt.Println(errJI.Error())
}
fmt.Println(string(jsonIndentBytes))
}
func post() {
ro := &grequests.RequestOptions{
JSON: []byte(`{"One":"Two"}`),
IsAjax: true,
}
resp, err := grequests.Post("http://httpbin.org/post", ro)
if err != nil {
log.Fatalln("Unable to make request: ", err)
}
if resp.Error != nil {
log.Fatalln("Unable to make request", resp.Error)
}
if resp.Ok != true {
log.Fatalln("Request did not return OK")
}
type PostJSONResponse struct {
Args struct{} `json:"args"`
Data string `json:"data"`
Files struct{} `json:"files"`
Form struct{} `json:"form"`
Headers struct {
AcceptEncoding string `json:"Accept-Encoding"`
ContentLength string `json:"Content-Length"`
ContentType string `json:"Content-Type"`
Host string `json:"Host"`
UserAgent string `json:"User-Agent"`
XRequestedWith string `json:"X-Requested-With"`
} `json:"headers"`
JSON struct {
One string `json:"One"`
} `json:"json"`
Origin string `json:"origin"`
URL string `json:"url"`
}
postJSONResponse := &PostJSONResponse{}
if err := resp.JSON(postJSONResponse); err != nil {
fmt.Println(err.Error())
}
// 打印构造结构
fmt.Println(postJSONResponse)
fmt.Println(postJSONResponse.URL)
// 打印转换内容 - Json
jsonBytes, errJ := json.Marshal(postJSONResponse) //转换成JSON返回的是byte[]
if errJ != nil {
fmt.Println(errJ.Error())
}
fmt.Println(string(jsonBytes))
// 打印转换内容 - Json 缩进
jsonIndentBytes, errJI := json.MarshalIndent(postJSONResponse, "", "\t") //转换成JSON返回的是byte[]
if errJI != nil {
fmt.Println(errJI.Error())
}
fmt.Println(string(jsonIndentBytes))
}
func upload() {
fd, err := grequests.FileUploadFromDisk("downloads/test.md")
if err != nil {
log.Fatalln("Unable to open file: ", err)
}
resp, _ := grequests.Post("http://httpbin.org/post",
&grequests.RequestOptions{
Files: fd,
Data: map[string]string{"One": "Two"},
})
if resp.Error != nil {
log.Fatalln("Unable to make request", resp.Error)
}
if resp.Ok != true {
log.Fatalln("Request did not return OK")
}
type PostFileUploadResponse struct {
Args struct{} `json:"args"`
Data string `json:"data"`
Files struct {
File string `json:"file"`
} `json:"files"`
Form struct {
One string `json:"one"`
} `json:"form"`
Headers struct {
AcceptEncoding string `json:"Accept-Encoding"`
ContentLength string `json:"Content-Length"`
ContentType string `json:"Content-Type"`
Host string `json:"Host"`
UserAgent string `json:"User-Agent"`
} `json:"headers"`
JSON interface{} `json:"json"`
Origin string `json:"origin"`
URL string `json:"url"`
}
postFileUploadResponse := &PostFileUploadResponse{}
if err := resp.JSON(postFileUploadResponse); err != nil {
log.Fatalln("Unable to coerce to JSON", err)
}
// 打印转换内容 - Json 缩进
jsonIndentBytes, errJI := json.MarshalIndent(postFileUploadResponse, "", "\t") //转换成JSON返回的是byte[]
if errJI != nil {
fmt.Println(errJI.Error())
}
fmt.Println(string(jsonIndentBytes))
}
func main() {
// 会话请求
//getSession()
// 基本请求
//get()
//post()
upload()
}