Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG] Middleware printing out "map[]" #780

Open
1 task done
kjagannath3 opened this issue Feb 2, 2025 · 2 comments
Open
1 task done

[BUG] Middleware printing out "map[]" #780

kjagannath3 opened this issue Feb 2, 2025 · 2 comments
Labels

Comments

@kjagannath3
Copy link

Is there an existing issue for this?

  • 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 kjagannath3 added the bug label Feb 2, 2025
@jackgris
Copy link

jackgris commented Feb 2, 2025

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.

@jub0bs
Copy link

jub0bs commented Mar 14, 2025

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants