在Go中将数据从前端提交到后端的方式

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

Submitting data from front end to back end in Go

问题

我有一个简单的网站,使用JavaScript编写。现在我收到了这个错误信息:"http: multiple response.WriteHeader calls",我知道我有另一个头部已经打开了,但我不知道在哪里,我正在努力寻找解决方案。

  1. func (t *Server) RootHandler(w http.ResponseWriter, r *http.Request) {
  2. var c *entities.Korisnik
  3. var k *entities.Kilometri
  4. var a *entities.Auto
  5. if c = t.authentication(w, r); c == nil {
  6. return
  7. }
  8. gk, err := t.store.GetKilometri(c)
  9. if errorEval(w, err, http.StatusInternalServerError) {
  10. return
  11. }
  12. if err := r.ParseForm(); err != nil {
  13. http.Error(w, err.Error(), http.StatusInternalServerError)
  14. return
  15. }
  16. if r.FormValue("debug") == "true" {
  17. s, _ := json.MarshalIndent(&Bla{c, gk}, "", " ")
  18. w.Write(s)
  19. return
  20. } else {
  21. w.Header().Set("Content-Type", "application/json")
  22. errorEval(w, json.NewEncoder(w).Encode(&Bla{c, gk}), http.StatusInternalServerError)
  23. }
  24. log.Println("0")
  25. if errorEval(w, json.NewDecoder(r.Body).Decode(&Input{a, k}), http.StatusBadRequest) {
  26. log.Println("1")
  27. return
  28. }
  29. err = t.store.NewKilometri(k, c, a)
  30. log.Println("2")
  31. if errorEval(w, err, http.StatusInternalServerError) {
  32. return
  33. }
  34. }
  35. func errorEval(w http.ResponseWriter, err error, status int) bool {
  36. if err == nil {
  37. return false
  38. }
  39. log.Println(err)
  40. http.Error(w, errorString[status], status)
  41. return true
  42. }

我在终端中得到了以下输出:

  1. 2015/10/20 16:12:32 0
  2. 2015/10/20 16:12:32 EOF
  3. 2015/10/20 16:12:32 http: multiple response.WriteHeader calls
  4. 2015/10/20 16:12:32 1
  5. 2015/10/20 16:12:32 0
  6. 2015/10/20 16:12:32 EOF
  7. 2015/10/20 16:12:32 http: multiple response.WriteHeader calls
  8. 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.

  1. func (t *Server) RootHandler(w http.ResponseWriter, r *http.Request) {
  2. var c *entities.Korisnik
  3. var k *entities.Kilometri
  4. var a *entities.Auto
  5. if c = t.authentication(w, r); c == nil {
  6. return
  7. }
  8. gk, err := t.store.GetKilometri(c)
  9. if errorEval(w, err, http.StatusInternalServerError) {
  10. return
  11. }
  12. if err := r.ParseForm(); err != nil {
  13. http.Error(w, err.Error(), http.StatusInternalServerError)
  14. return
  15. }
  16. if r.FormValue("debug") == "true" {
  17. s, _ := json.MarshalIndent(&Bla{c, gk}, "", " ")
  18. w.Write(s)
  19. return
  20. } else {
  21. w.Header().Set("Content-Type", "application/json")
  22. errorEval(w, json.NewEncoder(w).Encode(&Bla{c, gk}), http.StatusInternalServerError)
  23. }
  24. log.Println("0")
  25. if errorEval(w, json.NewDecoder(r.Body).Decode(&Input{a, k}), http.StatusBadRequest) {
  26. log.Println("1")
  27. return
  28. }
  29. err = t.store.NewKilometri(k, c, a)
  30. log.Println("2")
  31. if errorEval(w, err, http.StatusInternalServerError) {
  32. return
  33. }
  34. }

I get this in my terminal

  1. 015/10/20 16:12:32 0
  2. 2015/10/20 16:12:32 EOF
  3. 2015/10/20 16:12:32 http: multiple response.WriteHeader calls
  4. 2015/10/20 16:12:32 1
  5. 2015/10/20 16:12:32 0
  6. 2015/10/20 16:12:32 EOF
  7. 2015/10/20 16:12:32 http: multiple response.WriteHeader calls
  8. 2015/10/20 16:12:32 1
  9. func errorEval(w http.ResponseWriter, err error, status int) bool {
  10. if err == nil {
  11. return false
  12. }
  13. log.Println(err)
  14. http.Error(w, errorString[status], status)
  15. 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.

huangapple
  • 本文由 发表于 2015年10月20日 22:35:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/33239552.html
匿名

发表评论

匿名网友

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

确定