-
Notifications
You must be signed in to change notification settings - Fork 6
/
request.go
51 lines (44 loc) · 837 Bytes
/
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
package rq
import (
"net/http"
"net/url"
"strings"
)
// ParseRequest parses Rq as http.Request
func (r *Rq) ParseRequest() (req *http.Request, err error) {
// url
u, err := url.Parse(r.URL)
if err != nil {
return
}
// query
qs := u.Query()
for key, values := range r.Query {
for _, value := range values {
qs.Add(key, value)
}
}
u.RawQuery = qs.Encode()
// body
reader := r.Body
if reader == nil && len(r.Form) > 0 {
body := url.Values{}
for key, values := range r.Form {
for _, value := range values {
body.Add(key, value)
}
}
reader = strings.NewReader(body.Encode())
}
req, err = http.NewRequest(r.Method, u.String(), reader)
if err != nil {
return
}
// header
for key, values := range r.Header {
for _, value := range values {
req.Header.Add(key, value)
}
}
return
}