-
Notifications
You must be signed in to change notification settings - Fork 43
/
request.go
52 lines (40 loc) · 929 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
52
package neo
import (
"encoding/json"
"io/ioutil"
"net/http"
)
// Wrapped http.Request. It contains utility methods for dealing with content of incomming http.Request instance.
type Request struct {
*http.Request
Params UrlParam
ResolvedRoute string
}
// Make cookie map.
func buildReqCookies(cookies []*http.Cookie) Cookie {
result := Cookie{}
for _, cookie := range cookies {
result[cookie.Name] = cookie
}
return result
}
// Make new Request instance
func makeRequest(req *http.Request) *Request {
request := &Request{
Request: req,
}
return request
}
// Parse incomming Body as JSON
func (r *Request) JsonBody(instance interface{}) error {
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
return decoder.Decode(instance)
}
func (r *Request) StringBody() (string, error) {
content, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", err
}
return string(content), nil
}