mux icon indicating copy to clipboard operation
mux copied to clipboard

Path variable not parsed

Open KGKallasmaa opened this issue 2 years ago • 2 comments

Describe the bug

A clear and concise description of what the bug is.

I'm trying to return information about a specific user. Somehow the path variable is not recognised

Versions

Go version: 1.19 package version: run v1.8.0

Steps to Reproduce

How can the bug be triggered?

  1. Create a simple server with a path variable endpoint
  2. Return the path variable to the user from that handler

NB When I replaced the id path variable with a specific nr (e.g. 123) then it returned a 400 error ("userId is required")

Expected behavior

What output or behaviour were you expecting instead?

The router recognises the path variable.

Code Snippets

A minimum viable code snippet can be useful! (use backticks to format it).

ENDPOINT /v1/user/{id}

HANDLER

func FindUserById() http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		id, ok := vars["id"]
		if !ok {
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte("userId is required"))
			return
		}
....
		w.WriteHeader(http.StatusOK)
	})
}

KGKallasmaa avatar Dec 09 '22 13:12 KGKallasmaa

Hi @KGKallasmaa Could you put complete code snippet here, as I can test it out it is working fine. This is the code I have used

package main

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

	"github.com/gorilla/handlers"
	"github.com/gorilla/mux"
)

func YourHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Ping!\n"))
}

func FindUserById(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, ok := vars["id"]
	if !ok {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte("userId is required"))
		return
	}
	fmt.Println("User id " + id)
	
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("User id" + id))
}

func main() {
	r := mux.NewRouter()
	// Routes consist of a path and a handler function.
	r.HandleFunc("/ping", YourHandler)
	r.HandleFunc("/user/{id}", FindUserById)
	// Bind to a port and pass our router in

	ch := handlers.CORS(handlers.AllowedOrigins([]string{"*"}))
	//log.Fatal(http.ListenAndServe(":8000", ch(r)))
	s := http.Server{
		Addr:         "localhost:8000",
		Handler:      ch(r),
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
		IdleTimeout:  120 * time.Second,
	}

	log.Fatal(s.ListenAndServe())
}

itsdeekay avatar Jul 18 '23 16:07 itsdeekay

I use this code:

package main

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

	"github.com/gorilla/mux"
)

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", HomeHandler)
	r.HandleFunc("/v1/user/{id}", FindUserById())

	s := http.Server{
		Addr:         "localhost:8000",
		Handler:      r,
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
		IdleTimeout:  120 * time.Second,
	}

	log.Fatal(s.ListenAndServe())
}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
	_, _ = w.Write([]byte("Hello"))
}

func FindUserById() http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		id, ok := vars["id"]
		if !ok {
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte("userId is required"))
			return
		}
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write([]byte(id))
	})
}

And works fine for me, I imagine the time that passed, you solved this problem, right?

jackgris avatar Jul 24 '23 00:07 jackgris