github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_func.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  package ghttp
     8  
     9  import (
    10  	"strings"
    11  
    12  	"github.com/zhongdalu/gf/g/encoding/gurl"
    13  	"github.com/zhongdalu/gf/g/util/gconv"
    14  )
    15  
    16  // 构建请求参数,参数支持任意数据类型,常见参数类型为string/map。
    17  // 如果参数为map类型,参数值将会进行urlencode编码;可以通过 noUrlEncode:true 参数取消编码。
    18  func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
    19  	m, urlEncode := gconv.Map(params), true
    20  	if len(m) == 0 {
    21  		return gconv.String(params)
    22  	}
    23  	if len(noUrlEncode) == 1 {
    24  		urlEncode = !noUrlEncode[0]
    25  	}
    26  	s := ""
    27  	for k, v := range m {
    28  		if len(encodedParamStr) > 0 {
    29  			encodedParamStr += "&"
    30  		}
    31  		s = gconv.String(v)
    32  		if urlEncode && len(s) > 6 && strings.Compare(s[0:6], "@file:") != 0 {
    33  			s = gurl.Encode(s)
    34  		}
    35  		encodedParamStr += k + "=" + s
    36  	}
    37  	return
    38  }