-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext.go
63 lines (56 loc) · 1.32 KB
/
ext.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
package greq
import (
"net/http"
gourl "net/url"
"github.com/gookit/goutil/arrutil"
)
// SetHeaders sets the key, value pairs from the given http.Header to the
// request. Values for existing keys are overwritten.
//
// TODO replace to goutil/netutil/httpreq.SetHeaders
func SetHeaders(req *http.Request, headers ...http.Header) {
for _, header := range headers {
for key, values := range header {
req.Header[key] = values
}
}
}
// SetHeaderMap to reqeust instance.
//
// TODO replace to goutil/netutil/httpreq.SetHeaderMap
func SetHeaderMap(req *http.Request, headerMap map[string]string) {
for k, v := range headerMap {
req.Header.Set(k, v)
}
}
// MergeURLValues merge url.Values by overwrite.
//
// values support: url.Values, map[string]string, map[string][]string
//
// TODO replace to goutil/netutil/httpreq.MergeURLValues
func MergeURLValues(uv gourl.Values, values ...any) gourl.Values {
if uv == nil {
uv = make(gourl.Values)
}
for _, v := range values {
switch tv := v.(type) {
case gourl.Values:
for k, vs := range tv {
uv[k] = vs
}
case map[string]any:
for k, v := range tv {
uv[k] = arrutil.AnyToStrings(v)
}
case map[string]string:
for k, v := range tv {
uv[k] = []string{v}
}
case map[string][]string:
for k, vs := range tv {
uv[k] = vs
}
}
}
return uv
}