@@ -136,53 +136,67 @@ func (c *Client) prepareRequest(method, url string, data ...interface{}) (req *h
136
136
if len (c .prefix ) > 0 {
137
137
url = c .prefix + gstr .Trim (url )
138
138
}
139
- param := ""
139
+ var params string
140
140
if len (data ) > 0 {
141
141
switch c .header ["Content-Type" ] {
142
142
case "application/json" :
143
143
switch data [0 ].(type ) {
144
144
case string , []byte :
145
- param = gconv .String (data [0 ])
145
+ params = gconv .String (data [0 ])
146
146
default :
147
147
if b , err := json .Marshal (data [0 ]); err != nil {
148
148
return nil , err
149
149
} else {
150
- param = gconv . UnsafeBytesToStr (b )
150
+ params = string (b )
151
151
}
152
152
}
153
153
case "application/xml" :
154
154
switch data [0 ].(type ) {
155
155
case string , []byte :
156
- param = gconv .String (data [0 ])
156
+ params = gconv .String (data [0 ])
157
157
default :
158
158
if b , err := gparser .VarToXml (data [0 ]); err != nil {
159
159
return nil , err
160
160
} else {
161
- param = gconv . UnsafeBytesToStr (b )
161
+ params = string (b )
162
162
}
163
163
}
164
164
default :
165
- param = httputil .BuildParams (data [0 ])
165
+ params = httputil .BuildParams (data [0 ])
166
166
}
167
167
}
168
168
if method == "GET" {
169
- // It appends the parameters to the url if http method is GET.
170
- if param != "" {
171
- if gstr .Contains (url , "?" ) {
172
- url = url + "&" + param
173
- } else {
174
- url = url + "?" + param
169
+ var bodyBuffer * bytes.Buffer
170
+ if params != "" {
171
+ switch c .header ["Content-Type" ] {
172
+ case
173
+ "application/json" ,
174
+ "application/xml" :
175
+ bodyBuffer = bytes .NewBuffer ([]byte (params ))
176
+ default :
177
+ // It appends the parameters to the url
178
+ // if http method is GET and Content-Type is not specified.
179
+ if gstr .Contains (url , "?" ) {
180
+ url = url + "&" + params
181
+ } else {
182
+ url = url + "?" + params
183
+ }
184
+ bodyBuffer = bytes .NewBuffer (nil )
175
185
}
186
+ } else {
187
+ bodyBuffer = bytes .NewBuffer (nil )
176
188
}
177
- if req , err = http .NewRequest (method , url , bytes . NewBuffer ( nil ) ); err != nil {
189
+ if req , err = http .NewRequest (method , url , bodyBuffer ); err != nil {
178
190
return nil , err
179
191
}
180
192
} else {
181
- if strings .Contains (param , "@file:" ) {
193
+ if strings .Contains (params , "@file:" ) {
182
194
// File uploading request.
183
- buffer := new (bytes.Buffer )
184
- writer := multipart .NewWriter (buffer )
185
- for _ , item := range strings .Split (param , "&" ) {
195
+ var (
196
+ buffer = bytes .NewBuffer (nil )
197
+ writer = multipart .NewWriter (buffer )
198
+ )
199
+ for _ , item := range strings .Split (params , "&" ) {
186
200
array := strings .Split (item , "=" )
187
201
if len (array [1 ]) > 6 && strings .Compare (array [1 ][0 :6 ], "@file:" ) == 0 {
188
202
path := array [1 ][6 :]
@@ -225,7 +239,7 @@ func (c *Client) prepareRequest(method, url string, data ...interface{}) (req *h
225
239
}
226
240
} else {
227
241
// Normal request.
228
- paramBytes := []byte (param )
242
+ paramBytes := []byte (params )
229
243
if req , err = http .NewRequest (method , url , bytes .NewReader (paramBytes )); err != nil {
230
244
return nil , err
231
245
} else {
@@ -236,7 +250,7 @@ func (c *Client) prepareRequest(method, url string, data ...interface{}) (req *h
236
250
if (paramBytes [0 ] == '[' || paramBytes [0 ] == '{' ) && json .Valid (paramBytes ) {
237
251
// Auto detecting and setting the post content format: JSON.
238
252
req .Header .Set ("Content-Type" , "application/json" )
239
- } else if gregex .IsMatchString (`^[\w\[\]]+=.+` , param ) {
253
+ } else if gregex .IsMatchString (`^[\w\[\]]+=.+` , params ) {
240
254
// If the parameters passed like "name=value", it then uses form type.
241
255
req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
242
256
}
0 commit comments