我不明白為什么func (h Header) Get(key string) stringin package httpin fileheader.go不能按預期工作。無論其大小寫如何,我都想獲取標題“SOAPAction”,但我只得到一個空字符串""。直接訪問按預期工作。soapAction1 := r.Header.Get("SOAPAction")soapAction2 := r.Header["SOAPAction"]fmt.Println("soapAction1: ", soapAction1, " , soapAction2:", soapAction2)// Got: soapAction: , soapAction2: [MySoapHeader]// Expected: soapAction: MySoapHeader , soapAction2: [MySoapHeader]// Get gets the first value associated with the given key. If// there are no values associated with the key, Get returns "".// It is case insensitive; textproto.CanonicalMIMEHeaderKey is// used to canonicalize the provided key. To use non-canonical keys,// access the map directly.func (h Header) Get(key string) string { return textproto.MIMEHeader(h).Get(key)}
2 回答

冉冉說
TA貢獻1877條經驗 獲得超1個贊
http.Header.Get
方法依賴于字符串轉換來獲取給定字符串的值。
轉換由textproto
包實現
https://golang.org/pkg/net/textproto/#CanonicalMIMEHeaderKey
CanonicalMIMEHeaderKey 返回 MIME 標頭鍵 s 的規范格式。規范化將第一個字母和連字符后的任何字母轉換為大寫;其余的都轉換為小寫。
你可以自己試試:
package main
import (
"fmt"
"net/textproto"
)
func main() {
fmt.Println(textproto.CanonicalMIMEHeaderKey("SOAPAction"))
}
它打?。篠oapaction
- 2 回答
- 0 關注
- 151 瀏覽
添加回答
舉報
0/150
提交
取消