forked from xiaojiaoyu100/cast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_body.go
executable file
·180 lines (151 loc) · 3.18 KB
/
request_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package cast
import (
"encoding/json"
"encoding/xml"
"io"
"mime/multipart"
"os"
"path/filepath"
"bytes"
"github.com/google/go-querystring/query"
)
type requestBody interface {
ContentType() string
Body() ([]byte, error)
}
type requestCustomBody struct {
payload []byte
contentType string
}
func (body *requestCustomBody) ContentType() string {
return body.contentType
}
func (body *requestCustomBody) Body() ([]byte, error) {
return body.payload, nil
}
type requestJSONBody struct {
payload interface{}
}
func (body *requestJSONBody) ContentType() string {
return applicationJSON
}
func (body *requestJSONBody) Body() ([]byte, error) {
if body.payload == nil {
return []byte{}, nil
}
a, ok := body.payload.([]byte)
if ok {
return a, nil
}
return json.Marshal(body.payload)
}
type requestFormURLEncodedBody struct {
payload interface{}
}
func (body *requestFormURLEncodedBody) ContentType() string {
return formURLEncoded
}
func (body *requestFormURLEncodedBody) Body() ([]byte, error) {
if body.payload == nil {
return []byte{}, nil
}
a, ok := body.payload.([]byte)
if ok {
return a, nil
}
values, err := query.Values(body.payload)
if err != nil {
return nil, err
}
return []byte(values.Encode()), nil
}
type requestXMLBody struct {
payload interface{}
}
func (body *requestXMLBody) Body() ([]byte, error) {
if body.payload == nil {
return []byte{}, nil
}
a, ok := body.payload.([]byte)
if ok {
return a, nil
}
return xml.Marshal(body.payload)
}
func (body *requestXMLBody) ContentType() string {
return applicationXML
}
type requestPlainBody struct {
payload string
}
func (body *requestPlainBody) ContentType() string {
return textPlain
}
func (body *requestPlainBody) Body() ([]byte, error) {
return []byte(body.payload), nil
}
// FormData represents multipart
type FormData struct {
FieldName string
Value string
FileName string
Path string
Reader io.Reader
}
type requestMultipartFormDataBody struct {
formData []*FormData
contentType string
}
func (body *requestMultipartFormDataBody) ContentType() string {
return body.contentType
}
func (body *requestMultipartFormDataBody) Body() ([]byte, error) {
buffer := &bytes.Buffer{}
w := multipart.NewWriter(buffer)
for _, data := range body.formData {
switch {
case data.Value != "":
if err := w.WriteField(data.FieldName, data.Value); err != nil {
return nil, err
}
default:
if data.FieldName == "" || data.FileName == "" {
continue
}
if data.Path == "" && data.Reader == nil {
continue
}
fw, err := w.CreateFormFile(data.FieldName, data.FileName)
if err != nil {
return nil, err
}
switch {
case len(data.Path) > 0:
path, err := filepath.Abs(data.Path)
if err != nil {
return nil, err
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
_, err = io.Copy(fw, f)
if err != nil {
return nil, err
}
case data.Reader != nil:
_, err := io.Copy(fw, data.Reader)
if err != nil {
return nil, err
}
}
}
}
err := w.Close()
if err != nil {
return nil, err
}
body.contentType = w.FormDataContentType()
return buffer.Bytes(), nil
}