You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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
The text was updated successfully, but these errors were encountered:
Is there an existing issue for this?
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"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/health", calculator.Alive).Methods("GET")
port := ":8080"
log.Printf("Server started on http://localhost%s", port)
}
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"
)
//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
The text was updated successfully, but these errors were encountered: