如何在运行在AppEngine上的Go服务器生成的响应中设置HTTP头?

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

How do I set up HTTP headers in responses generated by Go server running AppEngine?

问题

我正在尝试使用golang和Google App Engine设置头部。以下是简单代码的样子:

w.Header().Set("Content-Type", "application/xml")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("header-name", "value")

在我的开发服务器上似乎不起作用。我总是得到通常的头部:

content-type:text/plain; charset=utf-8

当我部署时,我得到:

Content-Type:text/html; charset=utf-8

我是做错了还是这是一个错误(另一个错误)?

英文:

I'm trying to set headers using golang and google app engine . Here's how the trivial code looks like:

w.Header().Set("Content-Type", "application/xml")
w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("header-name", "value")

It seems that it's not working on my dev server. I always get the usual headers and

content-type:text/plain; charset=utf-8

When I deploy I get

Content-Type:text/html; charset=utf-8

Am I doing it wrong or this is a bug (another one)?

答案1

得分: 5

似乎问题是我在设置自定义头部之前设置了HTTP代码。请注意,如果在w.Header之前使用w.WriteHeader(200),则头部将不会被设置。

英文:

It seems the issue was that I set the http code before the custom headers. Be aware that if you have w.WriteHeader(200) before w.Header the headers will not be set.

答案2

得分: 0

GAE SDK存在一个明显的错误。在开发环境和生产环境中观察到了不合理的不同行为。我遇到了同样的问题,在设置特定顺序的头部时,在开发环境中可以正常工作,但在生产环境中头部没有设置。

在开发环境中,以下代码可以使你设置的头部正确返回:

func SignalingHandler(w http.ResponseWriter, r *http.Request) {
    if r.Method != "GET" {
        http.NotFound(w, r)
        return
    }

    data := "test"

    w.Write([]byte(data))

    w.Header().Set("Content-Type", "application/json")
    w.Header().Add("Access-Control-Allow-Origin", "*")
}

然而,在生产环境中,你必须反转顺序,并且只在设置完头部后使用 "Write" 方法(否则它不起作用):

func SignalingHandler(w http.ResponseWriter, r *http.Request) {
    if r.Method != "GET" {
        http.NotFound(w, r)
        return
    }

    data := "test"

    w.Header().Set("Content-Type", "application/json")
    w.Header().Add("Access-Control-Allow-Origin", "*")

    w.Write([]byte(data))
}

为了使生产环境正常工作,请确保在设置任何头部后不使用 "Write" 方法。

英文:

There's definitely a bug with the GAE SDK. Unjustified different behaviour is observed between dev and prod environments. I am experiencing the same issue, where I set the headers at a specific order - works on dev but in prod headers are not set.

In dev env, this makes the headers you set return properly:

func SignalingHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.NotFound(w, r)
		return
	}

	data := "test"

	w.Write([]byte(data))

	w.Header().Set("Content-Type", "application/json")
	w.Header().Add("Access-Control-Allow-Origin", "*")

}

However in prod environment, I have to reverse the order - and use the "write" method only AFTER setting the headers (otherwise it won't work):

 func SignalingHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.NotFound(w, r)
		return
	}

	data := "test"


	w.Header().Set("Content-Type", "application/json")
	w.Header().Add("Access-Control-Allow-Origin", "*")

	w.Write([]byte(data))
}

To make things work in prod, make sure you do not use the write method after setting any headers.

答案3

得分: 0

没有提供其他代码很难预测。然而,可能的根本原因是在设置头部之前你已经编写了一个响应。

可能是这样的:

w.Write([]byte(data))

或者是这样的:

fmt.Printf(w, "一个测试响应")
英文:

Without the rest of the code is hard to predict. However, the underlying reason is likely to be that you already wrote a response before setting the headers.

Either something like this:

w.Write([]byte(data))

Or something like this:

fmt.Printf(w, "A test response")

huangapple
  • 本文由 发表于 2014年4月4日 02:18:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/22845781.html
匿名

发表评论

匿名网友

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

确定