英文:
Submitting data from front end to back end in Go
问题
我有一个简单的网站,使用JavaScript编写。现在我收到了这个错误信息:"http: multiple response.WriteHeader calls",我知道我有另一个头部已经打开了,但我不知道在哪里,我正在努力寻找解决方案。
func (t *Server) RootHandler(w http.ResponseWriter, r *http.Request) {
var c *entities.Korisnik
var k *entities.Kilometri
var a *entities.Auto
if c = t.authentication(w, r); c == nil {
return
}
gk, err := t.store.GetKilometri(c)
if errorEval(w, err, http.StatusInternalServerError) {
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.FormValue("debug") == "true" {
s, _ := json.MarshalIndent(&Bla{c, gk}, "", " ")
w.Write(s)
return
} else {
w.Header().Set("Content-Type", "application/json")
errorEval(w, json.NewEncoder(w).Encode(&Bla{c, gk}), http.StatusInternalServerError)
}
log.Println("0")
if errorEval(w, json.NewDecoder(r.Body).Decode(&Input{a, k}), http.StatusBadRequest) {
log.Println("1")
return
}
err = t.store.NewKilometri(k, c, a)
log.Println("2")
if errorEval(w, err, http.StatusInternalServerError) {
return
}
}
func errorEval(w http.ResponseWriter, err error, status int) bool {
if err == nil {
return false
}
log.Println(err)
http.Error(w, errorString[status], status)
return true
}
我在终端中得到了以下输出:
2015/10/20 16:12:32 0
2015/10/20 16:12:32 EOF
2015/10/20 16:12:32 http: multiple response.WriteHeader calls
2015/10/20 16:12:32 1
2015/10/20 16:12:32 0
2015/10/20 16:12:32 EOF
2015/10/20 16:12:32 http: multiple response.WriteHeader calls
2015/10/20 16:12:32 1
英文:
I have simple website. Go with JavaScript. Now I get this message "http: multiple response.WriteHeader calls" and I know that a have another header open. But I don't know where and I'm struggling to find a solution.
func (t *Server) RootHandler(w http.ResponseWriter, r *http.Request) {
var c *entities.Korisnik
var k *entities.Kilometri
var a *entities.Auto
if c = t.authentication(w, r); c == nil {
return
}
gk, err := t.store.GetKilometri(c)
if errorEval(w, err, http.StatusInternalServerError) {
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.FormValue("debug") == "true" {
s, _ := json.MarshalIndent(&Bla{c, gk}, "", " ")
w.Write(s)
return
} else {
w.Header().Set("Content-Type", "application/json")
errorEval(w, json.NewEncoder(w).Encode(&Bla{c, gk}), http.StatusInternalServerError)
}
log.Println("0")
if errorEval(w, json.NewDecoder(r.Body).Decode(&Input{a, k}), http.StatusBadRequest) {
log.Println("1")
return
}
err = t.store.NewKilometri(k, c, a)
log.Println("2")
if errorEval(w, err, http.StatusInternalServerError) {
return
}
}
I get this in my terminal
015/10/20 16:12:32 0
2015/10/20 16:12:32 EOF
2015/10/20 16:12:32 http: multiple response.WriteHeader calls
2015/10/20 16:12:32 1
2015/10/20 16:12:32 0
2015/10/20 16:12:32 EOF
2015/10/20 16:12:32 http: multiple response.WriteHeader calls
2015/10/20 16:12:32 1
func errorEval(w http.ResponseWriter, err error, status int) bool {
if err == nil {
return false
}
log.Println(err)
http.Error(w, errorString[status], status)
return true
}
答案1
得分: 1
根据我的理解,errorEval
函数可能会在遇到错误时写入状态码和可能的响应体。在大多数调用该函数的地方,你都会检查返回值,并在处理错误时从处理程序中返回。
但是,在errorEval(w, json.NewEncoder(w).Encode(&Bla{c, gk}), http.StatusInternalServerError)
这个调用中,你没有检查返回值。
我猜测,可能存在某个 JSON 错误,处理程序正在写入一个 500 响应,并且你继续执行其他操作,这些操作又尝试写入额外的响应。
英文:
It looks to me like errorEval
likely writes the status code and maybe a body if an error is encountered. Most places you call it you check the return and return from your handler if it handles an error.
In the case of errorEval(w, json.NewEncoder(w).Encode(&Bla{c, gk}), http.StatusInternalServerError)
you are not checking the return.
My guess is, there is some json error and the handler is writing a 500 response, and you are continuing to do other things which in turn try to write additional responses.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论