forked from Greyh4t/zhttp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbody.go
152 lines (128 loc) · 3.45 KB
/
body.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package zhttp
import (
"bytes"
"encoding/json"
"encoding/xml"
"io"
"net/url"
"strings"
)
type Body interface {
Reader() (io.Reader, string, error)
}
// Raw used to create Body object from string, need to set the Content-Type yourself
func Raw(body string) Body {
return &StringBody{Body: body}
}
// RawBytes used to create Body object from []byte, need to set the Content-Type yourself
func RawBytes(body []byte) Body {
return &BytesBody{Body: body}
}
// RawReader used to create Body object from io.Reader, need to set the Content-Type yourself
func RawReader(body io.Reader) Body {
return &ReaderBody{Body: body}
}
// RawJSON used to create Body object from string, and set json Content-Type
func RawJSON(body string) Body {
return &StringBody{
ContentType: "application/json",
Body: body,
}
}
// RawBytesJSON used to create Body object from []byte, and set json Content-Type
func RawBytesJSON(body []byte) Body {
return &BytesBody{
ContentType: "application/json",
Body: body,
}
}
// JSON used to create Body object from map, struct and so on, and set json Content-Type
func JSON(body interface{}) Body {
return &JSONBody{body}
}
// RawXML used to create Body object from string, and set xml Content-Type
func RawXML(body string) Body {
return &StringBody{
ContentType: "application/xml",
Body: body,
}
}
// RawBytesXML used to create Body object from []byte, and set xml Content-Type
func RawBytesXML(body []byte) Body {
return &BytesBody{
ContentType: "application/xml",
Body: body,
}
}
// XML used to create Body object from struct, and set xml Content-Type
func XML(body interface{}) Body {
return &XMLBody{body}
}
// RawForm used to create Body object from string, and set form Content-Type
func RawForm(body string) Body {
return &StringBody{
ContentType: "application/x-www-form-urlencoded",
Body: body,
}
}
// RawBytesForm used to create Body object from []byte, and set form Content-Type
func RawBytesForm(body []byte) Body {
return &BytesBody{
ContentType: "application/x-www-form-urlencoded",
Body: body,
}
}
// Form used to create Body object from map, and set form Content-Type
func Form(body map[string]string) Body {
values := &url.Values{}
for key, value := range body {
values.Set(key, value)
}
return &StringBody{
ContentType: "application/x-www-form-urlencoded",
Body: values.Encode(),
}
}
type ReaderBody struct {
ContentType string
Body io.Reader
}
func (body *ReaderBody) Reader() (io.Reader, string, error) {
return body.Body, body.ContentType, nil
}
type StringBody struct {
ContentType string
Body string
}
func (body *StringBody) Reader() (io.Reader, string, error) {
return strings.NewReader(body.Body), body.ContentType, nil
}
type BytesBody struct {
ContentType string
Body []byte
}
func (body *BytesBody) Reader() (io.Reader, string, error) {
return bytes.NewReader(body.Body), body.ContentType, nil
}
type JSONBody struct {
Body interface{}
}
func (body *JSONBody) Reader() (io.Reader, string, error) {
contentType := "application/json"
data, err := json.Marshal(body.Body)
if err != nil {
return nil, "", err
}
return bytes.NewReader(data), contentType, nil
}
type XMLBody struct {
Body interface{}
}
func (body *XMLBody) Reader() (io.Reader, string, error) {
contentType := "application/xml"
data, err := xml.Marshal(body.Body)
if err != nil {
return nil, "", err
}
return bytes.NewReader(data), contentType, nil
}