Golang:为什么在这段代码中,response.Get(“headerkey”)没有返回值?

huangapple go评论159阅读模式
英文:

Golang: Why does response.Get("headerkey") not return a value in this code?

问题

这个问题困扰了我几个小时了,我试图获取一个响应头的值。很简单的事情。如果我使用curl发送一个请求到正在运行的服务器,我可以看到设置了头部,使用curl的-v标志,但是当我尝试使用Go的response.Header.Get()方法来获取头部时,它显示为空字符串"",头部的长度为0。

更让我沮丧的是,当我打印出响应体时,头部值实际上是设置好的(如下所示)。

非常感谢任何关于这个问题的帮助。

我有以下代码:
http://play.golang.org/p/JaYTfVoDsq

其中包含以下内容:

package main

import (
	"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()
}

运行这段代码的输出是:

auth header=, with length=0
res.Body=map[Authorization:[responseAuthVal] User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]
英文:

This has been bothering me for the past couple hours, I'm trying to get a response header value. Simple stuff. If I curl a request to this running server, I see the header set, with curl's -v flag, but when I try to retrieve the header using Go's response.Header.Get(), it shows a blank string "", with the header's length being 0.

What frustrates me even more, is that the header value is actually set within the response when I print out the body (as demonstrated below).

Any and all help with this is appreciated, thanks in advance.

I have this code here:
http://play.golang.org/p/JaYTfVoDsq

Which contains the following:

package main

import (
	"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()
}

The output to this running code is:

auth header=, with length=0
res.Body=map[Authorization:[responseAuthVal] User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]

答案1

得分: 6

这行代码:

        r.Header.Set("Authorization", "responseAuthVal")

设置了r *http.Request的值,即传入的请求,而你想要设置的是w http.ResponseWriter的值,即将要接收的响应。

应该将该行代码修改为:

        w.Header().Set("Authorization", "responseAuthVal")

参考这个 playground。

英文:

This line:

        r.Header.Set("Authorization", "responseAuthVal")

set the value of r *http.Request, that is the incomming request, while you want to set the value of w http.ResponseWriter, the response that you will receive.

The said line should be

        w.Header().Set("Authorization", "responseAuthVal")

See this playgroud.

huangapple
  • 本文由 发表于 2015年9月8日 17:28:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/32454079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定