gf/g/net/ghttp/ghttp_func.go

39 lines
1.1 KiB
Go
Raw Normal View History

// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// 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,
// You can obtain one at https://github.com/gogf/gf.
package ghttp
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"
"github.com/gogf/gf/g/util/gconv"
)
// 构建请求参数参数支持任意数据类型常见参数类型为string/map。
2019-07-11 19:47:15 +08:00
// 如果参数为map类型参数值将会进行urlencode编码可以通过 noUrlEncode:true 参数取消编码。
func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
m, urlEncode := gconv.Map(params), true
if len(m) == 0 {
return gconv.String(params)
}
2019-07-11 19:47:15 +08:00
if len(noUrlEncode) == 1 {
urlEncode = !noUrlEncode[0]
}
s := ""
2019-06-19 09:06:52 +08:00
for k, v := range m {
if len(encodedParamStr) > 0 {
encodedParamStr += "&"
}
s = gconv.String(v)
if urlEncode && len(s) > 6 && strings.Compare(s[0:6], "@file:") != 0 {
s = gurl.Encode(s)
2019-06-19 09:06:52 +08:00
}
encodedParamStr += k + "=" + s
2019-06-19 09:06:52 +08:00
}
return
}