-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
55 lines (42 loc) · 1.16 KB
/
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
53
54
55
package bowtie
import (
"encoding/json"
"io/ioutil"
"net/http"
)
// Struct Request adds a few convenience functions to `http.Request`.
type Request struct {
*http.Request
}
// NewRequest creates a new request instance. This is called transparently for you
// at the time the server receives a request
func NewRequest(r *http.Request) *Request {
return &Request{r}
}
// StringBody returns the request's body as a string
func (r *Request) StringBody() (string, error) {
if r.Body != nil {
res, err := ioutil.ReadAll(r.Body)
return string(res), err
}
return "", nil
}
// JSONBody attempts to unmarshal JSON out of the request's body, and
// returns a map if successful, or an error if not.
func (r *Request) JSONBody() (map[string]interface{}, error) {
if r.Body != nil {
res := map[string]interface{}{}
err := json.NewDecoder(r.Body).Decode(&res)
return res, err
}
return map[string]interface{}{}, nil
}
// ReadJSONBody attempts to unmarshal JSON from the request's body into
// a destination of your choosing.
func (r *Request) ReadJSONBody(v interface{}) error {
if r.Body != nil {
err := json.NewDecoder(r.Body).Decode(&v)
return err
}
return nil
}