Skip to content

Commit 4d32733

Browse files
committed
improve ghttp.Client for gogf#1179
1 parent 0e58b6e commit 4d32733

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

net/ghttp/internal/client/client_request.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,53 +136,67 @@ func (c *Client) prepareRequest(method, url string, data ...interface{}) (req *h
136136
if len(c.prefix) > 0 {
137137
url = c.prefix + gstr.Trim(url)
138138
}
139-
param := ""
139+
var params string
140140
if len(data) > 0 {
141141
switch c.header["Content-Type"] {
142142
case "application/json":
143143
switch data[0].(type) {
144144
case string, []byte:
145-
param = gconv.String(data[0])
145+
params = gconv.String(data[0])
146146
default:
147147
if b, err := json.Marshal(data[0]); err != nil {
148148
return nil, err
149149
} else {
150-
param = gconv.UnsafeBytesToStr(b)
150+
params = string(b)
151151
}
152152
}
153153
case "application/xml":
154154
switch data[0].(type) {
155155
case string, []byte:
156-
param = gconv.String(data[0])
156+
params = gconv.String(data[0])
157157
default:
158158
if b, err := gparser.VarToXml(data[0]); err != nil {
159159
return nil, err
160160
} else {
161-
param = gconv.UnsafeBytesToStr(b)
161+
params = string(b)
162162
}
163163
}
164164
default:
165-
param = httputil.BuildParams(data[0])
165+
params = httputil.BuildParams(data[0])
166166
}
167167
}
168168
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)
175185
}
186+
} else {
187+
bodyBuffer = bytes.NewBuffer(nil)
176188
}
177-
if req, err = http.NewRequest(method, url, bytes.NewBuffer(nil)); err != nil {
189+
if req, err = http.NewRequest(method, url, bodyBuffer); err != nil {
178190
return nil, err
179191
}
180192
} else {
181-
if strings.Contains(param, "@file:") {
193+
if strings.Contains(params, "@file:") {
182194
// 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, "&") {
186200
array := strings.Split(item, "=")
187201
if len(array[1]) > 6 && strings.Compare(array[1][0:6], "@file:") == 0 {
188202
path := array[1][6:]
@@ -225,7 +239,7 @@ func (c *Client) prepareRequest(method, url string, data ...interface{}) (req *h
225239
}
226240
} else {
227241
// Normal request.
228-
paramBytes := []byte(param)
242+
paramBytes := []byte(params)
229243
if req, err = http.NewRequest(method, url, bytes.NewReader(paramBytes)); err != nil {
230244
return nil, err
231245
} else {
@@ -236,7 +250,7 @@ func (c *Client) prepareRequest(method, url string, data ...interface{}) (req *h
236250
if (paramBytes[0] == '[' || paramBytes[0] == '{') && json.Valid(paramBytes) {
237251
// Auto detecting and setting the post content format: JSON.
238252
req.Header.Set("Content-Type", "application/json")
239-
} else if gregex.IsMatchString(`^[\w\[\]]+=.+`, param) {
253+
} else if gregex.IsMatchString(`^[\w\[\]]+=.+`, params) {
240254
// If the parameters passed like "name=value", it then uses form type.
241255
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
242256
}

0 commit comments

Comments
 (0)