-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrequest.go
More file actions
63 lines (53 loc) · 862 Bytes
/
request.go
File metadata and controls
63 lines (53 loc) · 862 Bytes
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 draft
import (
"net/http"
"net/url"
)
// Request -
type Request struct {
req *http.Request
Path string
Params *RequestParams
}
// RequestParams -
type RequestParams struct {
query url.Values
form url.Values
}
// ParseRequest -
func ParseRequest(r *http.Request) *Request {
p := &RequestParams{
query: r.URL.Query(),
form: nil,
}
if r.Method == http.MethodPost {
err := r.ParseForm()
if err == nil {
p.form = r.Form
}
}
return &Request{
req: r,
Path: r.URL.Path,
Params: p,
}
}
// Has -
func (p *RequestParams) Has(key string) bool {
if p.form != nil {
if _, ok := p.form[key]; ok {
return ok
}
}
_, ok := p.query[key]
return ok
}
// Get -
func (p *RequestParams) Get(key string) string {
if p.form != nil {
if v, ok := p.form[key]; ok {
return v[0]
}
}
return p.query.Get(key)
}