1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- // Copyright GoFrame Author(https://goframe.org). 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 httputil
- import (
- "github.com/gogf/gf/text/gstr"
- "net/http"
- "strings"
- "github.com/gogf/gf/encoding/gurl"
- "github.com/gogf/gf/util/gconv"
- )
- const (
- fileUploadingKey = "@file:"
- )
- // BuildParams builds the request string for the http client. The <params> can be type of:
- // string/[]byte/map/struct/*struct.
- //
- // The optional parameter <noUrlEncode> specifies whether ignore the url encoding for the data.
- func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
- // If given string/[]byte, converts and returns it directly as string.
- switch v := params.(type) {
- case string, []byte:
- return gconv.String(params)
- case []interface{}:
- if len(v) > 0 {
- params = v[0]
- } else {
- params = nil
- }
- }
- // Else converts it to map and does the url encoding.
- m, urlEncode := gconv.Map(params), true
- if len(m) == 0 {
- return gconv.String(params)
- }
- if len(noUrlEncode) == 1 {
- urlEncode = !noUrlEncode[0]
- }
- // If there's file uploading, it ignores the url encoding.
- if urlEncode {
- for k, v := range m {
- if gstr.Contains(k, fileUploadingKey) || gstr.Contains(gconv.String(v), fileUploadingKey) {
- urlEncode = false
- break
- }
- }
- }
- s := ""
- for k, v := range m {
- if len(encodedParamStr) > 0 {
- encodedParamStr += "&"
- }
- s = gconv.String(v)
- if urlEncode && len(s) > 6 && strings.Compare(s[0:6], fileUploadingKey) != 0 {
- s = gurl.Encode(s)
- }
- encodedParamStr += k + "=" + s
- }
- return
- }
- // HeaderToMap coverts request headers to map.
- func HeaderToMap(header http.Header) map[string]interface{} {
- m := make(map[string]interface{})
- for k, v := range header {
- if len(v) > 1 {
- m[k] = v
- } else {
- m[k] = v[0]
- }
- }
- return m
- }
|