-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
94 lines (74 loc) · 1.51 KB
/
common.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
package captchago
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
func cookiesToString(input map[string]string) string {
var output string
for k, v := range input {
output += k + "=" + v + "; "
}
if len(output) > 0 {
output = output[:len(output)-2]
}
return output
}
func postJSON(url string, data map[string]interface{}) (map[string]interface{}, error) {
jsonData, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var output map[string]interface{}
err = json.Unmarshal(body, &output)
if err != nil {
return nil, err
}
return output, err
}
func postQuery(link string, data map[string]interface{}) (string, error) {
var querys string
if data != nil {
for k, v := range data {
str := ""
switch v.(type) {
case string:
str = v.(string)
break
case int:
str = strconv.Itoa(v.(int))
break
case float64:
str = fmt.Sprintf("%.3f", v.(float64))
break
}
querys += url.QueryEscape(k) + "=" + url.QueryEscape(str) + "&"
}
if len(querys) > 0 {
querys = "?" + querys[:len(querys)-1]
}
}
resp, err := http.Get(link + querys)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}