-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
39 lines (33 loc) · 979 Bytes
/
parse.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
package httpc
import (
"encoding/xml"
"io"
"net/http"
jsoniter "github.com/json-iterator/go"
"gopkg.in/yaml.v2"
)
// Copy copies the response body into any io.Writer
func Copy(w io.Writer) func(resp *http.Response) error {
return func(resp *http.Response) error {
_, err := io.Copy(w, resp.Body)
return err
}
}
// ParseJSON parses the response body as JSON into a struct
func ParseJSON(v interface{}) func(resp *http.Response) error {
return func(resp *http.Response) error {
return jsoniter.NewDecoder(resp.Body).Decode(v)
}
}
// ParseYAML parses the response body as YAML into a struct
func ParseYAML(v interface{}) func(resp *http.Response) error {
return func(resp *http.Response) error {
return yaml.NewDecoder(resp.Body).Decode(v)
}
}
// ParseXML parses the response body as XML into a struct
func ParseXML(v interface{}) func(resp *http.Response) error {
return func(resp *http.Response) error {
return xml.NewDecoder(resp.Body).Decode(v)
}
}