英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论