mux icon indicating copy to clipboard operation
mux copied to clipboard

[BUG] Middleware printing out "map[]"

Open kjagannath3 opened this issue 11 months ago • 2 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Current Behavior

I am new to API development in GOLANG, and I am trying to set up a simple "hello world" api to get started. It should just have one endpoint that will return a string message when hit. I have a custom middleware function that prints out the method, time taken for request, the host, and the url path. When I test things out, the custom middleware executes correctly but in the following line, "map[]" is printed out. Upon further debugging, I can't seem to figure out where this erronious print is coming from.

Expected Behavior

This "map[]" string should not print out. I just want to see the fstring implemented in the middleware handler.

Steps To Reproduce

With the following code for router setup: package main

import ( "calculator-api/calculator" "log" "net/http" "time"

"github.com/gorilla/mux"

)

func main() { router := mux.NewRouter() router.HandleFunc("/health", calculator.Alive).Methods("GET") port := ":8080" log.Printf("Server started on http://localhost%s", port)

if err := http.ListenAndServe(port, router); err != nil {
	log.Fatal(err)
}

}

func loggingMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() log.Printf("[%s] %s %s %s", r.Method, time.Since(start), r.Host, r.URL.Path) next.ServeHTTP(w, r) }) }`,

And the following endpoint handler: import ( "fmt" "net/http"

"github.com/gorilla/mux"

)

//Simple health check endpoint func alive(w http.ResponseWriter,r *http.Request) { vars := mux.Vars(r) w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Response: %v\n", vars["message"]) } `, simply execute Go run, and hit the endpoint using curl/postman

Anything else?

No response

kjagannath3 avatar Feb 02 '25 17:02 kjagannath3

Hi @kjagannath3,

I'm not sure where you're seeing the map[] output. I modified the code to place everything in the same package:

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/gorilla/mux"
)

func main() {
	router := mux.NewRouter()
	router.HandleFunc("/health", loggingMiddleware(alive)).Methods("GET")
	port := ":8080"
	log.Printf("Server started on http://localhost%s", port)

	if err := http.ListenAndServe(port, router); err != nil {
		log.Fatal(err)
	}

}

func loggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()
		log.Printf("[%s] %s %s %s", r.Method, time.Since(start), r.Host, r.URL.Path)
		next.ServeHTTP(w, r)
	})
}

// Simple health check endpoint
func alive(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	w.WriteHeader(http.StatusOK)
	fmt.Fprintf(w, "Response: %v\n", vars["message"])
}

This is the log output from the server:

2025/02/02 20:00:38 Server started on http://localhost:8080
2025/02/02 20:00:40 [GET] 553ns localhost:8080 /health
2025/02/02 20:00:41 [GET] 490ns localhost:8080 /health
2025/02/02 20:14:23 [GET] 670ns localhost:8080 /health

And this is the output from curl:

curl -X GET http://localhost:8080/health
Response: 

The only way the handler prints an empty map is when you call the Fprintf method without specifying any key in the map, like this:

// Simple health check endpoint
func alive(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	w.WriteHeader(http.StatusOK)
	fmt.Fprintf(w, "Response: %v\n", vars)
}

And that is the correct behavior.

jackgris avatar Feb 02 '25 23:02 jackgris

Without additional information from the OP, this issue could likely be closed.

jub0bs avatar Mar 14 '25 09:03 jub0bs