2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-01-11 16:06:42 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-01-11 16:06:42 +08:00
|
|
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
2018-09-26 18:44:30 +08:00
|
|
|
|
import (
|
2019-07-11 19:47:15 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
"github.com/gogf/gf/g/encoding/gurl"
|
2019-04-23 20:12:44 +08:00
|
|
|
|
"github.com/gogf/gf/g/util/gconv"
|
2018-09-26 18:44:30 +08:00
|
|
|
|
)
|
2018-01-11 16:06:42 +08:00
|
|
|
|
|
2019-04-23 20:12:44 +08:00
|
|
|
|
// 构建请求参数,参数支持任意数据类型,常见参数类型为string/map。
|
2019-07-11 19:47:15 +08:00
|
|
|
|
// 如果参数为map类型,参数值将会进行urlencode编码;可以通过 noUrlEncode:true 参数取消编码。
|
|
|
|
|
func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
|
2019-07-11 10:02:44 +08:00
|
|
|
|
m, urlEncode := gconv.Map(params), true
|
2019-04-23 20:12:44 +08:00
|
|
|
|
if len(m) == 0 {
|
|
|
|
|
return gconv.String(params)
|
|
|
|
|
}
|
2019-07-11 19:47:15 +08:00
|
|
|
|
if len(noUrlEncode) == 1 {
|
|
|
|
|
urlEncode = !noUrlEncode[0]
|
2019-07-11 10:02:44 +08:00
|
|
|
|
}
|
2019-04-23 20:12:44 +08:00
|
|
|
|
s := ""
|
2019-06-19 09:06:52 +08:00
|
|
|
|
for k, v := range m {
|
|
|
|
|
if len(encodedParamStr) > 0 {
|
|
|
|
|
encodedParamStr += "&"
|
|
|
|
|
}
|
|
|
|
|
s = gconv.String(v)
|
2019-07-11 10:02:44 +08:00
|
|
|
|
if urlEncode && len(s) > 6 && strings.Compare(s[0:6], "@file:") != 0 {
|
|
|
|
|
s = gurl.Encode(s)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
2019-07-11 10:02:44 +08:00
|
|
|
|
encodedParamStr += k + "=" + s
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|