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") //添加请求头