Http middle-ware access to response

Hi,

i am relatively new to go.
i have written a logging middle-ware in order log some stuff.

func LoggingMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		startTime := time.Now()
		next.ServeHTTP(w, r)
		log.WithFields(log.Fields{"method": r.Method, "route": r.URL.String(), "status": "???", "time": time.Since(startTime)}).Debugln()
	})
}

I am trying to get the status from the http.Responsewriter.
Do i have to cast the writer to something in order to achieve this?

@netixen how do i get from w http.ResponseWriter -> Response -> w.Status? like i said i am new and there is a missing link between them.

As far as I can tell you cannot and you have to implement the ResponseWriter interface to wrap the original one to intercept the stuff you’re interested in.
The Gorilla project has a handler package with a logging handler that demonstrates this at https://github.com/gorilla/handlers/blob/master/handlers.go#L102

1 Like

@Bjoern is correct, the response I was thinking of is what you get on the client side.

@Bjoern it seems that i have to study how gorilla does it and where in the pipeline the wrapper is been used.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.