亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法獲取 HTTP 標頭

無法獲取 HTTP 標頭

Go
米脂 2022-07-18 16:39:10
我不明白為什么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貢獻1155條經驗 獲得超0個贊

文檔(強調添加):

Get 獲取與給定鍵關聯的第一個值。如果沒有與鍵關聯的值,Get 返回“”。它不區分大小寫;textproto.CanonicalMIMEHeaderKey 用于規范化提供的密鑰。要使用非規范鍵,請直接訪問地圖。

您的標頭 ,SOAPAction不是規范的,因此您有兩種選擇:

  1. 使用規范版本 ( Soapaction)

  2. 使用直接訪問,正如文檔所解釋的那樣,正如您所做的那樣。


查看完整回答
反對 回復 2022-07-18
?
冉冉說

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


查看完整回答
反對 回復 2022-07-18
  • 2 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號