问题描述
我正在编写一个向 WebSocket 网关发送请求的 HTTP API。我正在使用 go1.14.7
和 gorilla/mux
v1.8.0
。代码将使用 GOOS=linux
GOARCH=arm
GOARM=7
进行交叉编译。
我的问题如下:
-
respondBadRequest()
即使我从不使用WriteHeader
也总是记录这个:http: superfluous response.WriteHeader call from main.(*HttpHandler).respondBadRequest
- API 响应始终为
200 OK
,即使调用respondBadRequest()
也是如此 - API 响应的正文始终为空
我对 Go 完全陌生。下面是我的代码结构。
type HttpHandler struct {
gateway Gateway
}
func (h *HttpHandler) respondSuccess(w http.ResponseWriter,text string) {
w.Write([]byte(text))
}
func (h *HttpHandler) respondBadRequest(w http.ResponseWriter,text string) {
http.Error(w,text,http.StatusBadRequest)
}
func (h *HttpHandler) respondError(w http.ResponseWriter,http.StatusInternalServerError)
}
func (h *HttpHandler) OnFoobar(w http.ResponseWriter,r *http.Request) {
f := func(success bool,e error) {
if e != nil {
h.respondError(w,e.Error())
} else if success {
h.respondSuccess(w,"Foobar Accepted")
} else {
h.respondBadRequest(w,"Unknown Foobar")
}
}
//...
e := h.gateway.Foobar(f)
if e != nil {
log.Println(e)
}
}
//...
httpHandler := &HttpHandler{
gateway: gateway,}
r := mux.NewRouter()
r.HandleFunc("/foobar",httpHandler.OnFoobar)
http.ListenAndServe(":8000",r)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)