5
return func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) return }
加了这样的代码,但前端接收到还是200的状态码,中间件w.WriteHeader(http.StatusBadRequest) return 代码无效
return func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) return }
加了这样的代码,但前端接收到还是200的状态码,中间件w.WriteHeader(http.StatusBadRequest) return 代码无效
给出完整代码哈,这看不出来是不是使用错误。
示例代码
server.Use(BadRequestHandler)
func BadRequestHandler(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
return
})
}
func Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "无效请求", http.StatusBadRequest)
return
}
}
我知道怎么弄了