這在過去幾個(gè)小時(shí)一直困擾著我,我正在嘗試獲取響應(yīng)標(biāo)頭值。簡單的東西。如果我curl向這個(gè)正在運(yùn)行的服務(wù)器發(fā)出請求,我會(huì)看到標(biāo)頭設(shè)置,帶有 curl 的-v標(biāo)志,但是當(dāng)我嘗試使用 Go's 檢索標(biāo)頭時(shí)response.Header.Get(),它顯示一個(gè)空白字符串"",標(biāo)頭的長度為 0。更讓我沮喪的是,當(dāng)我打印出正文時(shí),標(biāo)題值實(shí)際上是在響應(yīng)中設(shè)置的(如下所示)。感謝您對此的任何幫助,提前致謝。我在這里有這個(gè)代碼:http : //play.golang.org/p/JaYTfVoDsq其中包含以下內(nèi)容:package mainimport ( "fmt" "io/ioutil" "net/http" "net/http/httptest")func main() { mux := http.NewServeMux() server := httptest.NewServer(mux) defer server.Close() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { r.Header.Set("Authorization", "responseAuthVal") fmt.Fprintln(w, r.Header) }) req, _ := http.NewRequest("GET", server.URL, nil) res, _:= http.DefaultClient.Do(req) headerVal := res.Header.Get("Authorization") fmt.Printf("auth header=%s, with length=%d\n", headerVal, len(headerVal)) content, _ := ioutil.ReadAll(res.Body) fmt.Printf("res.Body=%s", content) res.Body.Close()}此運(yùn)行代碼的輸出是:auth header=, with length=0res.Body=map[Authorization:[responseAuthVal] User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]
- 1 回答
- 0 關(guān)注
- 253 瀏覽
添加回答
舉報(bào)
0/150
提交
取消