-
Notifications
You must be signed in to change notification settings - Fork 0
/
req_params.go
57 lines (48 loc) · 1008 Bytes
/
req_params.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
package util
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
)
type ReqParams struct {
url.Values
Body []byte
}
func NewReqParams(req *http.Request) (*ReqParams, error) {
reqParams, err := url.ParseQuery(req.URL.RawQuery)
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
return &ReqParams{reqParams, data}, nil
}
func (r *ReqParams) Get(key string) (string, error) {
v, ok := r.Values[key]
if !ok {
return "", errors.New("key not in query params")
}
return v[0], nil
}
func (r *ReqParams) GetAll(key string) ([]string, error) {
v, ok := r.Values[key]
if !ok {
return nil, errors.New("key not in query params")
}
return v, nil
}
type PostParams struct {
*http.Request
}
func (p *PostParams) Get(key string) (string, error) {
if p.Request.Form == nil {
p.Request.ParseMultipartForm(1 << 20)
}
if vs, ok := p.Request.Form[key]; ok {
return vs[0], nil
}
return "", errors.New("key not in post params")
}