Introduction to VPS and Web Technology Development

Http header 索引大小写敏感问题

自由vps golang

问题如下:

curl -i --location --request POST 'https://api.xxx.com/coupon/info/list'
--header 'accessKey:  AEf'
--header 'accessToken:  EE0BB88263F73D19F5074E9D1910739F'
--header 'timestamp: 1666087720519'
--header 'Content-Type: application/json'
--data '{
    "channelCode": "TMALL",
    "couponStatus": [
        15
    ],
    "pageNo": 1,
    "pageSize": 3,
    "source": "TMALL",
    "asid": "tmJDDFI003"
}'

在宿主机内请求  accessKey  可以在接收方得到原样的索引值,但是在容器内获取请求,接收方却变成了 accesskey

经过上面的curl  -i信息得知道  宿主机内走的http1.1协议  ,容器内走的却是http2协议
在容器内加上  --http1.1  指定http协议版本如下:
curl --http1.1 -i --location --request POST 'https://api.xxx.com/coupon/info/list'
--header 'accessKey:  AEf'
--header 'accessToken:  EE0BB88263F73D19F5074E9D1910739F'
--header 'timestamp: 1666087720519'
--header 'Content-Type: application/json'
--data '{
    "channelCode": "TMALL",
    "couponStatus": [
        15
    ],
    "pageNo": 1,
    "pageSize": 3,
    "source": "TMALL",
    "asid": "tmJDDFI003"
}'

接收方获取的数据正常保持原样key
经查相关文档: http1.1对大小写不敏感,但是会在传输过程中保持原样的索引,http2则是会自动转成符合rfc标准的索引值

查相关的Golang代码,发现是因为域名走的https协议,代码层面,

http.transport默认会对https协议进行http2强制尝试,所以有下面改良代码,对https的安全验证跳过检查
tls.Config{InsecureSkipVerify: true}
,因为对方实际上使用的http1.1

//跳过证书验证
 tr := &http.Transport{
   TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }
request.Header = make(http.Header)
request.Header = header
request.Header.Set("Content-Type", "application/json;charset=UTF-8") //添加请求头  client := http.Client{
   Timeout: time.Second * 10,  Transport: tr, } //创建客户端 resp, err := client.Do(request.WithContext(context.TODO())) //发送请求 if resp == nil && err != nil { return nil, err
}


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

免责声明:

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

同类推荐
评论列表