Introduction to VPS and Web Technology Development

golang header传输大小写的问题

自由vps golang
golang的http库遵循rfc的标准协议 ,会将所有的header参数使用大驼峰的方式进行下标修正
header.set方法或者add方法都在标准库中使用下面这个函数对下标进行修正
// CanonicalMIMEHeaderKey returns the canonical format of the // MIME header key s. The canonicalization converts the first // letter and any letter following a hyphen to upper case; // the rest are converted to lowercase. For example, the // canonical key for "accept-encoding" is "Accept-Encoding". // MIME header keys are assumed to be ASCII only. // If s contains a space or invalid header field bytes, it is // returned without modifications. func CanonicalMIMEHeaderKey(s string) string {
   commonHeaderOnce.Do(initCommonHeader) // Quick check for canonical encoding.  upper := true  for i := 0; i < len(s); i++ {
      c := s[i] if !validHeaderFieldByte(c) { return s
      } if upper && 'a' <= c && c <= 'z' { return canonicalMIMEHeaderKey([]byte(s))
      } if !upper && 'A' <= c && c <= 'Z' { return canonicalMIMEHeaderKey([]byte(s))
      }
      upper = c == '-'  } return s
}



可以使用以下方法进行修正头部参数:

header := make(http.Header)
header[`accessToken`] = []string{accessToken}
header[`timestamp`] = []string{unixTime}
header[`accessKey`] = []string{this.chiefcloudsCoupon.AccessKey}
buffer := bytes.NewBuffer(paramsBytes)
request, err := http.NewRequest("POST", this.chiefcloudsCoupon.Domain, buffer) if err != nil { return nil, errors.New("三方接口配置异常")
}
request.Header = make(http.Header)
request.Header = header
request.Header.Set("Content-Type", "application/json;charset=UTF-8") //添加请求头


使用chatGPT寻求答案
标签: 暂无标签

免责声明:

本站提供的资源,都来自网络,版权争议与本站无关,所有内容及软件的文章仅限用于学习和研究目的。不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负,我们不保证内容的长久可用性,通过使用本站内容随之而来的风险与本站无关,您必须在下载后的24个小时之内,从您的电脑/手机中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。侵删请致信E-mail:master@freevpsweb.com

同类推荐
评论列表