无法获取HTTP头

无法获取HTTP头,http,dictionary,go,header,Http,Dictionary,Go,Header,我不明白为什么包http中的func(h Header)Get(key string)string文件Header.go不能按预期工作 我想得到标题“SOAPAction”,而不考虑其大小写,但我只得到一个空字符串“”。直接访问按预期工作 soapAction1:=r.Header.Get(“SOAPAction”) soapAction2:=r.Header[“SOAPAction”] fmt.Println(“soapAction1:”,soapAction1,”,soapAction2:,

我不明白为什么包
http
中的
func(h Header)Get(key string)string
文件
Header.go
不能按预期工作

我想得到标题“SOAPAction”,而不考虑其大小写,但我只得到一个空字符串
”。直接访问按预期工作

soapAction1:=r.Header.Get(“SOAPAction”)
soapAction2:=r.Header[“SOAPAction”]
fmt.Println(“soapAction1:”,soapAction1,”,soapAction2:,soapAction2)
//Got:soapAction:,soapAction2:[MySoapHeader]
//预期:soapAction:MySoapHeader,soapAction2:[MySoapHeader]
//Get获取与给定键关联的第一个值。如果
//没有与键关联的值,Get返回“”。
//它不区分大小写;textproto.canonicalTimeHeaderKey为
//用于规范化提供的密钥。要使用非规范键,
//直接访问地图。
func(h头)Get(键字符串)字符串{
返回textproto.MIMEHeader(h).Get(键)
}
来自(增加强调):

Get获取与给定键关联的第一个值。如果没有与键关联的值,Get将返回“”。它不区分大小写textproto.CanonicalTimeHeaderKey用于规范化提供的密钥。要使用非规范密钥,请直接访问映射。

您的标题
SOAPAction
,不是cannonical,因此您有两个选择:

  • 使用规范版本(
    Soapaction
  • 使用直接访问,正如文档所解释的,正如您所做的那样

  • http.Header.Get
    方法依赖字符串转换来获取给定字符串的值

    转换由
    textproto
    包实现

    CanonicalTimeHeaderKey返回MIME头的规范格式 基斯。规范化转换第一个字母和任何字母 在连字符后面加上大写字母其余部分转换为小写

    你可以自己试试:

    package main
    
    import (
        "fmt"
        "net/textproto"
    )
    
    func main() {
        fmt.Println(textproto.CanonicalMIMEHeaderKey("SOAPAction"))
    }
    
    它打印:
    Soapaction