I'm trying to develop an api gateway using go-chi. I want that if request includes "_always200=true" in query, I will set status code to 200. Here what I tried: custom_response_writer.go:
type CustomResponseWriter struct {
http.ResponseWriter
Buf *bytes.Buffer
StatusCode int
WroteHeader bool
}
func NewCustomResponseWriter(w http.ResponseWriter) *CustomResponseWriter {
return &CustomResponseWriter{ResponseWriter: w, Buf: new(bytes.Buffer)}
}
func (c *CustomResponseWriter) WriteHeader(code int) {
c.StatusCode = code
c.ResponseWriter.WriteHeader(code)
}
func (c *CustomResponseWriter) Write(b []byte) (int, error) {
return c.Buf.Write(b)
}
middleware:
func HeaderFilterMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
always200, _ := r.Context().Value(always200QueryKey).(bool)
crw := NewCustomResponseWriter(w)
next.ServeHTTP(crw, r)
if always200 {
crw.WriteHeader(http.StatusOK)
}
})
}
It is always returns 404. Is there any way to fix that ?
There are at least two issues in the code.
always200, _ := r.Context().Value(always200QueryKey).(bool)
is not the correct way to get the query parameter. I think you should read it withr.URL.Query().Get("_always200")
and compare the value to the string"true"
.it's too late to call
crw.WriteHeader(http.StatusOK)
afternext.ServeHTTP(crw, r)
. It printed this warning message when I tested the code:Here is an amended example that changes the status code of a response: