-
Notifications
You must be signed in to change notification settings - Fork 36
/
request.go
90 lines (83 loc) · 2.1 KB
/
request.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
package jas
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
const (
baseLocal = "http://localhost/"
)
func NewGetRequest(baseUrlOrPath, path string, nameValues ...interface{}) *http.Request {
url := concateUrl(baseUrlOrPath, path)
if len(nameValues) > 0 {
query := NameValuesToUrlValues(nameValues...).Encode()
url += "?" + query
}
req, _ := http.NewRequest(
"GET", url, nil,
)
return req
}
func NewPostFormRequest(baseUrlOrPath, path string, nameValues ...interface{}) *http.Request {
var reader io.Reader
if len(nameValues) > 0 {
pairs := NameValuesToUrlValues(nameValues...)
reader = strings.NewReader(pairs.Encode())
}
req, _ := http.NewRequest("POST", concateUrl(baseUrlOrPath, path), reader)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req
}
func NewPostJsonRequest(baseUrlOrPath, path string, jsonData []byte, nameValues ...interface{}) *http.Request {
url := concateUrl(baseUrlOrPath, path)
if len(nameValues) > 0 {
query := NameValuesToUrlValues(nameValues...).Encode()
url += "?" + query
}
req, _ := http.NewRequest(
"POST", url, bytes.NewReader(jsonData),
)
req.Header.Set("Content-Type", "application/json")
return req
}
func NameValuesToUrlValues(nameValues ...interface{}) url.Values {
if len(nameValues)%2 != 0 {
panic(fmt.Sprint("name value pair not even:", nameValues, len(nameValues)))
}
nameValuesStrings := make([]string, len(nameValues))
for i := 0; i < len(nameValues); i++ {
value := nameValues[i]
var str string
if byt, ok := value.([]byte); ok {
str = string(byt)
} else {
str = fmt.Sprint(value)
}
nameValuesStrings[i] = str
}
values := url.Values{}
for i := 0; i < len(nameValuesStrings); i += 2 {
values.Set(nameValuesStrings[i], nameValuesStrings[i+1])
}
return values
}
func concateUrl(base, path string) string {
if base == "" {
base = baseLocal
} else if !strings.HasPrefix(base, "http") {
if base[0] == '/' {
base = base[1:]
}
base = baseLocal + base
}
if base[len(base)-1] != '/' {
base += "/"
}
if path != "" && path[0] == '/' {
path = path[1:]
}
return base + path
}