1. 首页
  2. 主题
  3. Go问与答

问一个关于http: multiple response.WriteHeader calls 错误的问题

haoyu_zjp · · 3691 次点击
首先,我使用了github.com/ant0ine/go-json-rest/rest 这个包 直接贴代码 ```go func Start(){ NeptuneLog := logs.New() api := rest.NewApi() api.Use(rest.DefaultDevStack...) router, err := rest.MakeRouter( rest.Get("/:type/:action", handle), rest.Post("/:type/:action", handle), ) if err != nil { NeptuneLog.Fatal(err) } api.SetApp(router) NeptuneLog.Fatal(http.ListenAndServe(":8080", api.MakeHandler())) } ``` =================start 负责调用 handle ==================== ```go func handle (w rest.ResponseWriter, r *rest.Request) { NeptuneLog := logs.New() t := r.PathParam("type") if (t == "submit") { action := r.PathParam("action") submitNode := SubmitNode{R:r,W:w,Action:action} SubmitChan <- submitNode NeptuneLog.Printf("%v",w) //node := test11{Action:action} //w.WriteJson(node) } else if t =="browser" { action := r.PathParam("action") browserNode := BrowserNode{R:r,W:w,Action:action} BrowserChan <- browserNode } else { //w.WriteJson("not found") } } ``` ============= handle 负责把request 和 response 放到结构体,然后放到channel ============ ```go func doSubmit () { NeptuneLog := logs.New() for item := range SubmitChan{ NeptuneLog.Println("111") node := test11{Action:item.Action} NeptuneLog.Printf("%v",node) NeptuneLog.Printf("%v",item.W) item.W.WriteJson(node) NeptuneLog.Printf("%v",item.W) } } ``` =========== dosubmit 真正干活,负责从channel拿东西,然后返回 ====== 调用之后,server打印出了 http: multiple response.WriteHeader calls 的错误 ==========更新下最新进展 ======== 我把打印的返回拿出来的 发现有 Conn.Write wrote more than the declared Content-Length 这个错误
看错误,多次调用输出头?响应头在哪输出的,检查下
#1
更多评论
我又做了几次测试,我觉得错误应该是,handle函数退出会导致这个请求被置为完成的状态,然后这个ResponseWriter变量就不可用了,然后又再去读写这个变量,那么就挂掉了
#2

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中